Search code examples
androidlistviewbaseadapternotifydatasetchanged

How does my Adapter know about notifydatasetchanged


I have a ListView, populated by a custom adapter:

public class myAdapter implements ListAdapter

This basic part is working - the views are created with the right data and all is good.

The data underlying the list will change occasionally, and I need to be able to cause the whole thing to refresh when it does. I am struggling to work out how to do this.

BaseAdapter provides notifyDataSetChanged(), so I can make my class extend BaseAdapter:

public class myAdapter extends BaseAdapter implements ListAdapter

but just calling notifyDataSetChanged() on my object does... well, nothing as far as I can see. Certainly the ListView doesn't get updated and myAdapter.getView() is not called.

Calling invalidate() on the ListView also appears to have no effect.

It seems likely that my adapter needs to know about the possibility of data changing but I don't understand how to tie it all in with the BaseAdapter and/or ListAdapter.


Solution

  • How does my Adapter know about notifydatasetchanged

    under the hook BaseAdapter implements The Observer patter, and notifyDataSetChanged, notify the observer.

    If you implements ListAdapter you have to provide your own version of registerDataObser and unregisterDataObserver . If you left those empty, notifyDatasetChanged does nothing. On the other hand BaseAdapter implements ListeAdapter and provides already the implementation for the above-quoted methods .