I do use this code for refill ListView with data after they change
// ListView lv;
// MyListAdapter la;
// DataClass dc;
dc.remove(object_id);
lv.setAdapter(la);
Is this the best way since we can’t use notifyDataSetChanged() which is available only in ArrayAdapter ?
Solution
//MyListAdapter.java
private ArrayList<DataSetObserver> observers = new ArrayList<DataSetObserver>();
public void registerDataSetObserver(DataSetObserver arg0) {
observers.add(arg0);
}
public void unregisterDataSetObserver(DataSetObserver arg0) {
observers.remove(arg0);
}
public void notifyDataSetChange() {
for (DataSetObserver d : observers) {
d.onChanged();
}
}
public void remove(int position) {
[DataClass object].remove(position);
notifyDataSetChange();
}
For a CursorAdapter I use the following code:
If you are using custom adapter or as you commented: only want ListAdapter as member variable. Instead of using
private CustomAdapter mAdapter;(which i would recommended to avoid creating unnecessary objects).You can use DataSetObserver part and ListAdapter.registerDataSetObserver(DataSetObserver observer). DataSetObserver.onChanged() will be called by the BaseAdapter implementation, so it should work for all adapter.
BaseAdapter.notifyDataSetChanged():