Clear listview content?
Simply write
listView.setAdapter(null);
I guess you passed a List or an Array to the Adapter. If you keep the instance of this added collection, you can do a
collection.clear();
listview.getAdapter().notifyDataSetChanged();
this'll work only if you instantiated the adapter with collection and it's the same instance.
Also, depending on the Adapter you extended, you may not be able to do this. SimpleAdapter is used for static data, thus it can't be updated after creation.
PS. not all Adapters have a clear() method. ArrayAdapter does, but ListAdapter or SimpleAdapter don't
It's simple .First you should clear your collection and after clear list like this code :
yourCollection.clear();
setListAdapter(null);
As of Android versions M and N, following works for me and would be the correct approach. Emptying the ListView or setting the Adapter to null is not the right approach and would lead to null pointer issue, invalid ListView and/or crash of the app.
Simply do:
mList.clear();
mAdapter.notifyDataSetChanged();
i.e. first you clear the list altogether, and then let the adapter know about this change. Android will take care of correctly updating the UI with an empty list. In my case, my list is an ArrayList.
In case you are doing this operation from a different thread, run this code on the UI thread:
runOnUiThread(mRunnable);
Where mRunnable would be:
Runnable mRunnable = new Runnable() {
public void run() {
mList.clear();
mAdapter.notifyDataSetChanged();
}
};;