Dynamically update the list using the adapter

This tutorial uses the SimpleAdapter, which works great, but I need to update the arrays in the adapter when entering new data.

Could you advise me how to update the ListView using something else than the SimpleAdapter?

+57
android listview
Mar 16 '11 at 2:22
source share
4 answers

Use the ArrayAdapter supported by ArrayList. To change the data, simply refresh the data in the list and call adapter.notifyDataSetChanged () .

+119
Mar 16 '11 at 2:29
source share

If you create your own adapter, there is one notable abstract function:

public void registerDataSetObserver(DataSetObserver observer) { ... } 

You can use these observers to notify you of a system update:

 private ArrayList<DataSetObserver> observers = new ArrayList<DataSetObserver>(); public void registerDataSetObserver(DataSetObserver observer) { observers.add(observer); } public void notifyDataSetChanged(){ for (DataSetObserver observer: observers) { observer.onChanged(); } } 

Although you are not happy that there are such things as SimpleAdapter and ArrayAdapter, and you do not need to do all this?

+26
Aug 16 '11 at 3:27
source share

SimpleListAdapter is mainly used for static data! If you want to process dynamic data, you are better off working with an ArrayAdapter , ListAdapter, or with a CursorAdapter if your data comes from a database.

Here's a helpful guide to understanding binding data in a ListAdapter

As pointed out in this question, https://stackoverflow.com/a/4646263

+3
Mar 16 2018-11-11T00:
source share

Most people recommend using notifyDataSetChanged() , but I found this link pretty useful. In fact, using clear and add , you can achieve the same goal using less memory and a more responsible application.

For example:

 notesListAdapter.clear(); notes = new ArrayList<Note>(); notesListAdapter.add(todayNote); if (birthdayNote != null) notesListAdapter.add(birthdayNote); /* no need to refresh, let the adaptor do its job */ 
0
Mar 06 '16 at 23:44
source share



All Articles