SimpleAdapter notifyDataSetChangedSimpleAdapter、notifyDataSetChanged

由网友(越努力越幸运)分享简介:我想刷新我的ListView每当我上删除项目。但设置 notifyDataSetChanged(); 是给我这个错误:I'm trying to refresh my ListView whenever I delete an item on it. But setting a notifyDataSetChange...

我想刷新我的ListView每当我上删除项目。但设置 notifyDataSetChanged(); 是给我这个错误:

I'm trying to refresh my ListView whenever I delete an item on it. But setting a notifyDataSetChanged(); is giving me this error:

不能引用非最终变量适配器的内部类中   在不同的方法中定义

Cannot refer to a non-final variable adapter inside an inner class defined in a different method.

该方法notifyDataSetChanged()是未定义的类型   ListAdapter。

The method notifyDataSetChanged() is undefined for the type 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;
                }
            });
}

然后,它提出将其改为:

Then it suggests to change it to:

((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});

但其更改到后不是刷新列表。如何解决这个?

But it's not refreshing the list after changing to that. What is the solution for this?

推荐答案

解决方案中。必须通知数据集改变之前先删除从列表视图的数组列表中的项目。

Solution found. Must remove first the item from the array list of the listview before notifying data set changed.

data.remove(position);
adapter.notifyDataSetChanged();
阅读全文

相关推荐

最新文章