Can somebody please give me an example code of removing all ListView items and replacing with new items?
I tried replacing the adapter items without success. My code is
populateList(){
results //populated arraylist with strings
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
listview.setOnItemClickListener(this);
}
// now populating list again
repopulateList(){
results1 //populated arraylist with strings
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results1);
listview.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listview.setOnItemClickListener(this);
}
Here repopulateList()
method will add to ListView items, but it doesn't remove/replace all ListView items.
then call
notifyDataSetChanged();
You will want to
remove()
the item from your adapter object and then just run thenotifyDatasetChanged()
on the Adapter, anyListView
s will (should) recycle and update on it's own.Here's a brief activity example with
AlertDialog
s:Remove it from the adapter and then notify the arrayadapter that data set has changed.
You can use
I think if u add the following code, it will work
To remove an item, Just remove that item from the arraylist that we passed to the adapter and do
listview.invalidateViews();
This will refresh the listview
At first you should remove the item from your list. Later you may empty your adapter and refill it with new list.