每当我删除ListView上的项目时,我都会尝试刷新它。但是设置一个notifyDataSetChanged();会给我带来这个错误:
不能引用在不同方法中定义的内部类中的非最终变量适配器。
方法notifyDataSetChanged()未为ListAdapter类型定义。
ArrayList <HashMap <String, String> > data = dataHolder.getAllData();
dataHolder.getAllData();
if(data.size() !=0){
ListView lv = (ListView) findViewById(R.id.datalist);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.dataentry, new String[]{"unique_id","lastName"}, new int[]{R.id.unique_id,R.id.last_name});
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setTitle("Delete?");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setMessage("Delete selected item?");
adb.setCancelable(false);
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Delete", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView uID = (TextView) findViewById(R.id.unique_id);
String unique_id = uID.getText().toString();
dataHolder.deleteData(unique_id);
adapter.notifyDataSetChanged();
}
});
adb.show();
return false;
}
});
}然后建议将其改为:
((BaseAdapter) adapter).notifyDataSetChanged();
final ListAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.dataentry, new String[]{"unique_id","lastName"}, new int[]{R.id.unique_id,R.id.last_name});但这并不是在更改列表之后刷新列表。解决这个问题的办法是什么?
发布于 2014-02-22 21:50:07
找到解决办法了。必须先从列表视图的数组列表中删除该项,然后才能通知更改的数据集。
data.remove(position);
adapter.notifyDataSetChanged();发布于 2014-01-30 10:28:42
要从adapter中引用AlertDialog.OnClickListener,you need it to be final (这基本上就是错误告诉您的)。所以只需声明它final
final BaseAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.dataentry, new String[]{"unique_id","lastName"}, new int[]{R.id.unique_id,R.id.last_name});您还可以从调用它的参数中获取它:
@Override
public boolean onItemLongClick(final AdapterView<?> adapterView, View arg1, int arg2, long arg3) {
// ...
final Adapter adapter = adapterView.getAdapter();
if (adapter instanceof BaseAdapter) {
((BaseAdapter)adapter).notifyDataSetChanged();
} else {
throw new RuntimeException("Unexpected adapter");
}例如。
发布于 2014-01-30 10:17:03
只需将ListAdapter声明为全局变量即可
https://stackoverflow.com/questions/21453511
复制相似问题