[英]Android: Using notifyDataSetChanged to update listview
I have a listview which I want to edit from a dialog box. 我有一个要从对话框中编辑的列表视图。 I've read from googling that I need to notifyDataSetChanged() on the listview, however when I try to do this I get an error:
我从谷歌搜索中了解到,我需要在列表视图上使用notifyDataSetChanged(),但是当我尝试执行此操作时,我得到了一个错误:
The method notifyDataSetChanged(View) is undefined for the type ListView
My listview is set originally at the top of my code with just a: 我的列表视图最初设置在代码的顶部,只是一个:
ListView listView;
It's then set in a routine on the onload 然后将其设置在onload的例程中
public void loadItems(){
//Removed - just getting the data
int rowCount;
rowCount = mCategory.size();
listView = (ListView) findViewById(R.id.lvItems);
int[] colors = {0, 0xFFFF0000, 0};
listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
listView.setDividerHeight(1);
listView.setAdapter(new CustomAdapter());
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
loadPopup(mDescription.get(position).toString(), mCountList.get(position).toString(), mTemplatecode.get(position).toString());
}
});
//Removed - Tidying up
}
The dialog box that is loaded on the onlick has a simple interface, a minus button, a plus button, a text box and a go button. onlick上加载的对话框具有简单的界面,减号按钮,加号按钮,文本框和执行按钮。 The number in the textbox is changed which does a db call.
文本框中的数字已更改,可以进行数据库调用。 When the dialog box closes I want it to refresh the listview behind to reflect the new change.
当对话框关闭时,我希望它刷新后面的列表视图以反映新的更改。 Ie basically re-run the loadItems() routine again.
即基本上再次重新运行loadItems()例程。
This is what happens when you click the go button on the dialog. 单击对话框上的“执行”按钮时,就会发生这种情况。 I'm obviously putting the notifyDataSetChanged in the wrong place as it wont even run.
我显然将notifyDataSetChanged放在错误的位置,因为它甚至不会运行。
btnGo.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View v) {
addAsset(v, AssetDesc, txt.getText().toString(), templateCode);
listView.notifyDataSetChanged();
loadItems();
dialog.dismiss();
}
});
Custom adapter: 自定义适配器:
class CustomAdapter extends BaseAdapter
{
@Override
public int getCount() {
return mDescription.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LayoutInflater inf=getLayoutInflater();
View v=inf.inflate(R.layout.noncriticalasset, arg2,false);
TextView tv=(TextView)v.findViewById(R.id.txtOption);
TextView tvCount=(TextView)v.findViewById(R.id.txtCount);
tv.setText(mDescription.get(arg0).toString());
tvCount.setText(mCountList.get(arg0).toString());
return v;
}
} }
您需要在适配器上而不是ListView本身上调用notifyDataSetChanged
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.