user3364963
user3364963

Reputation: 397

Android: How to clear a list view without removing data from the array list?

I am attempting to create a list view which displays different data based on a selection from an a spinner.

My list view is implemented via a base adapter which works fine.

My data is stored in an array list of objects which each contains fields that store individual data and based on the selection from the spinner filters the data stored in the array list and displays it on the list view.

But my question is how can I clear a list view content without clearing its underlying data (stored in the array list) and calling notifyDataSetChanged? I want to be able to preserve the data stored so if the user goes back to a previous selection, the data is still present.

Thank you for your time in reading this.

Upvotes: 2

Views: 1968

Answers (3)

Marco Acierno
Marco Acierno

Reputation: 14847

You could just pass another ArrayList to the adapter with the new data to show inside the ListView.

Or, you could create two ArrayList inside the Adapter, one named original which contains the original elements of the ListView and another currentElements which will be the real ArrayList to show changed as you want. With some custom methods, you can choose to switch between the two versions of the ArrayList.

It's could be the same concept of an Adapter which implement a Filter.

Anyway if i understand your problem, you want to change the content of a ListView based on the selection. Well, in this case i would send another ArrayList to show.

Upvotes: 2

wvdz
wvdz

Reputation: 16651

Create a list of adapters, one adapter for each selection. Then simply switch to a different adapter when the selection is changed with listView.setAdapter(adapter)

Upvotes: 2

G.T.
G.T.

Reputation: 1557

As your data is stored into an ArrayList, you can simply use:

listView.setAdapter(null);

It will remove the adapter of your ListView, and so, remove all the items displayed.

The data will still be in your ArrayList so if you want to re-display this ListView, you just have to recreate your Adapter and set it again. You can even create a global instance of your BaseAdapter so you only have to create it once.

Upvotes: 3

Related Questions