[英]Android API ListView doesn't update when using notifyDataSetChanged();
[英]Android: Using notifyDataSetChanged to update listview
我有一个要从对话框中编辑的列表视图。 我从谷歌搜索中了解到,我需要在列表视图上使用notifyDataSetChanged(),但是当我尝试执行此操作时,我得到了一个错误:
The method notifyDataSetChanged(View) is undefined for the type ListView
我的列表视图最初设置在代码的顶部,只是一个:
ListView listView;
然后将其设置在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
}
onlick上加载的对话框具有简单的界面,减号按钮,加号按钮,文本框和执行按钮。 文本框中的数字已更改,可以进行数据库调用。 当对话框关闭时,我希望它刷新后面的列表视图以反映新的更改。 即基本上再次重新运行loadItems()例程。
单击对话框上的“执行”按钮时,就会发生这种情况。 我显然将notifyDataSetChanged放在错误的位置,因为它甚至不会运行。
btnGo.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View v) {
addAsset(v, AssetDesc, txt.getText().toString(), templateCode);
listView.notifyDataSetChanged();
loadItems();
dialog.dismiss();
}
});
自定义适配器:
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.