I have ArrayList<MyObject> data
which is populated on the ListView
using a CustomAdapter
.
I've implemented View.OnClickListener
in the CustomAdapter
class and clicking a button deletes the particular row. After deleting the row, both from database and from the data
object using data.remove(i)
inside the Custom Adapter, I call notifyDataSetChanged()
.
Problem is that it does not refresh teh ListView
.
I'm using a tabbed activity with fragment. So, when go to some distant tab and come back to this tab, it refreshes the ListView
and the deleted item does not show anymore.
Why is the ListView
not refreshing immediately when i call notifyDataSetChanged()
but changing when i move to another fragment and back?
My CustomAdapter
class is as follows:
public class CustomAdapter extends BaseAdapter implements View.OnClickListener {
private LayoutInflater mInflater;
Context context;
private ArrayList<MyObject> data;
public CustomAdapter(Context context, ArrayList<MyObject> data) {
mInflater = LayoutInflater.from(context);
this.data = data;
this.context = context;
}
.....
.....
.....
.....
.....
@Override
public void onClick(View v) {
DataBaseHelper db = new DataBaseHelper(context);
int id = data.get((Integer) v.getTag()).id;
//use getTag() to get the position, set position in tag before adding listener.
switch (v.getId()){
case R.id.txtName:
Toast.makeText(context, "Show Details" + v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.btnRemove:
db.removePaper(id); // works fine
data.remove(v.getTag()); // works fine
notifyDataSetChanged(); // PROBLEM HERE: Does not refresh listview
break;
case R.id.btnFavorite:
Toast.makeText(context, "Favorite" + v.getTag(), Toast.LENGTH_SHORT).show();
break;
}
}
}