Search code examples
androidnotifydatasetchanged

notifyDataSetChanged not work in my activity


in my code when delete row of list view list view don't change.i use adapter.notifyDataSetChange() but it not word.this is my code : code make different position of class.

CustomList adapter;
Integer[] imageId;
public String[] _Data2;
 public int positionAll;
ArrayList<ArrayList<String>> _Data = new ArrayList<ArrayList<String>>();
DataBase data = new DataBase(Show_Code.this, "MELK_TBL");


 try {
        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();
    } catch (Exception e) {
        Toast.makeText(getApplication(), e.toString(), Toast.LENGTH_LONG).show();
    }

    list.setAdapter(adapter);



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.get(position).get(2), _Data.get(position).get(3), _Data.get(position).get(4), _Data.get(position).get(5), _Data.get(position).get(6), _Data.get(position).get(7));
                data.close();
                adapter.notifyDataSetChanged();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });

please help me,i don't any time for it :(


Solution

  • 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 :)