notifyDataSetChanged not work in my activity

不想你离开。 提交于 2019-12-02 13:06:14

After deleting the values you need to pass the new arraylist in which you had removed all those values and then notify the adapter class. In your case see the below code

list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                                       int position, long id) {

            try {
                data.open();
                data.Delete(_Data.get(position).get(1));
                data.close();
                **//Edited code...**
                 _Data.get(position).remove(1);
                 adapter.refreshView(_Data);
                 **//Edited code...**
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });

And in adapter class refreshview method will be like below,

    public void refreshView(String[] _Data) {
    this._Data = _Data;
     notifyDataSetChanged();
    }

By this way you can notify the data. For example I have deleted only one value and notify.

Hope this is helpful :)

As the size of Array is not changed at run time so u need to create new adapter and set again to list You have to add this code in onItemLongClick

data.open();
        _Data = data.GetData();
        imageId = new Integer[_Data.size()];
        _Data2 = new String[_Data.size()];
        for (int i = 0; i < _Data.size(); i++) {
            imageId[i] = R.drawable.municipal;
            _Data2[i] = _Data.get(i).get(1) + "_" + _Data.get(i).get(2) + "_" + _Data.get(i).get(3) + "_" + _Data.get(i).get(4) + "_" + _Data.get(i).get(5) + "_" + _Data.get(i).get(6) + "_0";
        }
        adapter = new CustomList(Show_Code.this, _Data2, imageId);
        data.close();
        list.setAdapter(adapter)

you are passing _Data2 object in adapter. You should update same object after deleting from data. Try adding this before data.close() in onItemLongClick(AdapterView<?> parent, View view,int position, long id) method:

_Data = data.GetData();
imageId = new Integer[_Data.size()];
_Data2.clear();
for (int i = 0; i < _Data.size(); i++) {
     imageId[i] = R.drawable.municipal;
     _Data2[i] = _Data.get(i).get(1) + "_" + _Data.get(i).get(2) + "_" + _Data.get(i).get(3) + "_" + _Data.get(i).get(4) + "_" + _Data.get(i).get(5) + "_" + _Data.get(i).get(6) + "_0";
}

Don't create new object of _Data2. Just clear same object and add whole data in it again and after that you can call adapter.notifyDataSetChanged() then this will get updated automatically.

You'll need to reorganize your code, using Methods, only for Select you databe again and do the list.setAdapter(adapter); then use adapter.notifyDataSetChanged();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!