John Greening
John Greening

Reputation: 13

how to refresh an android listview

I realize that similar questions have been asked before. My list is displayed using

setListAdapter(new ArrayAdapter<String>(this, R.layout.listlayout, names));

to refresh, previous answers have either been to use notifyDataSetChanged() or invalidate() but to what !? I don't appear to have anything to apply the method to.

Sorry if I'm being daft.

Upvotes: 0

Views: 2229

Answers (4)

Peter Knego
Peter Knego

Reputation: 80330

First you need to have a reference to ArrayAdapter:

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listlayout, names);
setListAdapter(adapter);

Then when you need to manipulate data you need to do it via ArrayAdapter methods add, insert, remove, etc.. When done you call adapter.NotifyDataSetChanged().

Upvotes: 1

Steve Bergamini
Steve Bergamini

Reputation: 14600

I like to declare my adapter as a member variable and call it off of that. But since you're not, you can do this:

listview.getAdapter().notifyDataSetChanged();

This assumes that your ListView object is called listView.

Upvotes: 0

kibibyte
kibibyte

Reputation: 3037

Store your ArrayAdapter somewhere. For example

adapter = new ArrayAdapter<String>(this, R.layout.listlayout, names)
setListAdapter(adapter);
...
adapter.notifyDataSetChanged();

Upvotes: 1

K-ballo
K-ballo

Reputation: 81349

To the list adapter.

ArrayAdapter<String> adapter = (ArrayAdapter<String>)yourListView.getAdapter();
adapter.notifyDataSetChanged();

Upvotes: 0

Related Questions