gpt4 book ai didi

android - 如何让我的自定义 ListAdapter 在 notifyDatasetChanged 上更新?

转载 作者:行者123 更新时间:2023-11-29 21:37:46 28 4
gpt4 key购买 nike

这是我第一次使用自定义布局构建 Listview,因此如果我遗漏了一些明显的内容,请指出。

我遇到的问题是我无法让 ListView 在 Oncreate() 之后使用新信息更新自身;已经用过。所以这个列表是非常静态的。

我正在尝试创建一个看起来像这样的自定义 ListView 适配器:

public class MainListCustomBaseAdapter extends BaseAdapter {

static ArrayList<ListItems> DataSomething;
static Context Cont;

public MainListCustomBaseAdapter (ArrayList<ListItems> data, Context c){
DataSomething = data;
Cont = c;
}

public int getCount() {
// TODO Auto-generated method stub
return DataSomething.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return DataSomething.get(position);
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)Cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainlistlayout, null);
}

ImageView image = (ImageView) v.findViewById(R.id.ListImage);
TextView titleView = (TextView)v.findViewById(R.id.title);
TextView DetailItemView = (TextView)v.findViewById(R.id.DetailItem);


ListItems msg = DataSomething.get(position);
image.setImageResource(msg.icon);
titleView.setText(msg.title);
DetailItemView.setText("ItemDetails: "+msg.ItemDetails);


return v;
}

public void updateResults(ArrayList<MainListCustomBaseAdapter> results){
notifyDataSetChanged();


}


}

我的 Oncreate 看起来像这样:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


RecipeList = (ListView) findViewById(R.id.mainListView);

ShoppingItems = new ArrayList<ListItems>();
RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));

ListItems Detail;
Detail = new ListItems();
Detail.setIcon(R.drawable.food);
Detail.setName("Food Stuff");
Detail.setItemDetails("ItemDetailsComp");
ShoppingItems.add(Detail);

}

listitem 看起来像这样:

public class ListItems {
public int icon ;
public String title;
public String ItemDetails;


public String getName() {
return title;
}

public void setName(String from) {
this.title = from;
}

public String getItemDetails() {
return ItemDetails;
}

public void setItemDetails(String ItemDetailsComp) {
this.ItemDetails = ItemDetailsComp;
}

public int getIcon() {
return icon;
}

public void setIcon(int icon) {
this.icon = icon;
}

}

如何让 ListView 动态更新?可能使用 SetInvalidatedViews() 或 notifyDatasetchanged()?

非常感谢任何帮助。

最佳答案

使用下面的

   MainListCustomBaseAdapter adapter = new MainListCustomBaseAdapter(ShoppingItems, this)
RecipeList.setAdapter(adapter);

刷新或更新 ListView

   adapter.notifyDataSetChanged();

public void notifyDataSetChanged ()

在 API 级别 1 中添加

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

关于android - 如何让我的自定义 ListAdapter 在 notifyDatasetChanged 上更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17969535/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com