1

I have a ListView that I'm using for some different adapters, is this right to use following code for removing my ListView adapter and clear the ListView ?

list.setAdapter(new ArrayAdapter<Void>(mContext, 0, new Void[]{}));

What is your offer? and what is the best way?

Code has been updated

Pejman
  • 2,442
  • 4
  • 34
  • 62

2 Answers2

1

Simply Set Listview.setAdapter(null)

Ashish Mohan
  • 118
  • 1
  • 4
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – lokusking Jun 29 '16 at 07:45
-1

clear the data in the resource (i.e) what ever your using in the arrayAdapter

madapter.notifyDataSetChanged();
KomalG
  • 808
  • 9
  • 18
  • I'm using multiple adapters and by clearing them I had to re-fill them again and it's taking time... – Pejman Jan 03 '15 at 09:54
  • maintain your arraylist(resource in the arrayAdapter) globally if you want to change the content in the list just the clear the data and add new Data to ArrayList and call madapter.notifyDataSetChanged(); it is little bit fast try it – KomalG Jan 03 '15 at 09:58
  • Is this so bad ??? : `list.setAdapter(new ArrayAdapter(mContext, 0, new Void[]{}));` WHY mate ?? – Pejman Jan 03 '15 at 09:59
  • in some situation, i need to make list empty, and shows a toast that saying "Nothing has found" list must be empty in some cases... – Pejman Jan 03 '15 at 10:00
  • can you explain what is the exact problem... updating the listView or showing Toast??? – KomalG Jan 03 '15 at 10:09
  • look, there is listview that i'm use for adding criteria for the search, and also im using the list for displaying search result, if search has no result, then list must be empty. and there is some other adapters they cant be clear, cause re-filling them taking time... – Pejman Jan 03 '15 at 10:11
  • check this link it will helps you :http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ – KomalG Jan 03 '15 at 10:15
  • Maintaining a global array is a really bad idea...especially if you are recommended Omid use filters. ANy filter operation creates a new List reference. Which means your external reference no longer points to what the adapter uses. Can read more about it here: http://www.jaysoyer.com/2014/07/constructor-woes-androids-arrayadapter/ – Ifrit Jan 03 '15 at 13:23