SimpleAdapter notifyDataSetChanged [英] SimpleAdapter notifyDataSetChanged

查看:308
本文介绍了SimpleAdapter notifyDataSetChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想刷新我的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();

这篇关于SimpleAdapter notifyDataSetChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆