-1

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 :(

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
amardco
  • 17
  • 1
  • 6

4 Answers4

3

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)
Rahul Giradkar
  • 1,818
  • 1
  • 17
  • 28
2

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.

Anuja Kothekar
  • 2,537
  • 2
  • 15
  • 28
  • 1
    will the size of _Data2 will be same as earlier?? No right? You should use ArrayList for it so that when it is updated it will automatically get updated in your adapter on notifyDataSetChange. – Anuja Kothekar Nov 10 '16 at 06:45
  • i change code by define _Data2 as arraylist and change such as your code but it not work again – amardco Nov 10 '16 at 07:22
0

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

Rodrigo Paixão
  • 246
  • 5
  • 12
  • Sure, but you need to reload the Object from your database, reset the Adapter with the current data, before use `notifyDataSetChanged()`. – Rodrigo Paixão Nov 09 '16 at 12:33
0

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

Jeevanandhan
  • 1,073
  • 10
  • 18
  • @amardco: You need remove the deleted value from the String array in the activity .i.e., in your case `_Data2` is the String array. So you have to remove the deleted values from the `_Data2` and need to replace the adapter String array value with the new array and then notify the adapter. It will work, this is what I have explained here. In your case you have deleted the value in the activity but you haven't passed that value to the adapter, that is why the notify is not working. – Jeevanandhan Nov 10 '16 at 07:21
  • @amardco: Pleasure is mine :) – Jeevanandhan Nov 10 '16 at 08:51