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 :(
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();
来源:https://stackoverflow.com/questions/40506747/notifydatasetchanged-not-work-in-my-activity