update listview dynamically with adapter
Asked Answered
T

5

71

This tutorial uses a SimpleAdapter which works fine, but I need to update the arrays in the adapter when new data is entered.

Could you please guide me on how to update a ListView using something other than a SimpleAdapter?

Toadeater answered 16/3, 2011 at 2:22 Comment(1)
check this question for more information #2251270Bricklayer
D
140

Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call adapter.notifyDataSetChanged().

Demur answered 16/3, 2011 at 2:29 Comment(1)
Beat me to it. :) Although you can call notifyDataSetChanged() on SimpleAdapter too.Amaris
R
26

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

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

You can use the given observers to notify the system to 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();
    }
}

Though aren't you glad there are things like the SimpleAdapter and ArrayAdapter and you don't have to do all that?

Ruction answered 16/8, 2011 at 3:27 Comment(0)
J
4

SimpleListAdapter's are primarily used for static data! If you want to handle dynamic data, you're better off working with an ArrayAdapter, ListAdapter or with a CursorAdapter if your data is coming in from the database.

Here's a useful tutorial in understanding binding data in a ListAdapter

As referenced in this SO question

Jeri answered 16/3, 2011 at 2:35 Comment(3)
I was able to update the SimpleAdapter with notifyDataSetChanged(). Your referenced link to the other SO question is also misleading, as that post also says you can't update the data in a SimpleAdapter. If you update the array data, and call notifiyDataSetChanged() on the SimpleAdapter, the data will be refreshed and updated on the UI.Toadeater
@Toadeater The SimpleList is meant to work with non-mutable data items. You're using a mutable data-source and manually notifying your Activity that the datasource has changed. This is more work than it's worth and could confuse other programmers. I've pointed you towards better Data Structures that are equipped to handle mutable data. If you don't want to work around it, fine, work harder, but my answer stands. And the SO question I link to you has your response as well as a modified answer. So it's valid as well.Jeri
Admittedly, when I first asked this question I knew nothing about Android and little about programming. Thank you for taking the time to guide me in the right direction.Toadeater
A
0

Most people recommend using notifyDataSetChanged(), but I found this link pretty useful. In fact using clear and add you can accomplish the same goal using less memory footprint, and more responsibe app.

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 */
Anitaanitra answered 6/3, 2016 at 23:44 Comment(1)
If you use .clear() here you will clear the entire List where you are trying to just update one of the list items and have the new content displayed. This won't workKalmuck
L
0

I created a method just for that. I use it any time I need to manually update a ListView. Hopefully this gives you an idea of how to implement your own

public static void UpdateListView(List<SomeObject> SomeObjects, ListView ListVw)
{    
    if(ListVw != null)
    {
        final YourAdapter adapter = (YourAdapter) ListVw.getAdapter();

        //You'll have to create this method in your adapter class. It's a simple setter.
        adapter.SetList(SomeObjects);

        adapter.notifyDataSetChanged();
    }
}

I'm using an adapter that inherites from BaseAdapter. Should work for any other type of adapter.

Leap answered 14/12, 2019 at 0:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.