0

onei have an ArrayAdapter that implements the list of autocomplete proposals for an EditText field.

AutoCompleteTextView ediFrom = (AutoCompleteTextView) findViewById(R.id.ediFrom);   
ArrayList<String> autoCompleteLib = new ArrayList<String>();
autoCompleteFromAdapter = new AutoCompleteAdapter(this, android.R.layout.simple_dropdown_item_1line, autoCompleteLib);
ediFrom.setAdapter(autoCompleteFromAdapter);

During runtime in a TimerTask (so a background thread) i'm adding elements to this list:

synchronized (lock)
{
autoCompleteLib.add(newAddress);
}

Now i want to update the autocomplete proposals list. Is it enough / correct to call "notifyDataSetChanged()" once after all adding action is done, or do i have call "notifyDataSetChanged()" after each "autoCompleteLib.add" ?

thx for your help

nr1
  • 777
  • 3
  • 12
  • 31

2 Answers2

3

If you want to update Adapter for each row added, call notifyDataSetChanged() for each row added. If not, call notifyDataSetChanged() after work is done.

It depends on what you want but i recommend you to call it only once. If you will have for example milion rows, it's pretty sick to call notify method each time when new item is added to Adapter.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
3

Is it enough / correct to call "notifyDataSetChanged()" once after all adding action is done, or do i have call "notifyDataSetChanged()" after each "autoCompleteLib.add" ?

notifyDataSetChanged() tells the ListView (or GridView, etc) to redraw the entire ViewGroup which can be slow and should only be used when you want to actually see each change.

If you are only adding a bunch of items in bulk, I recommend calling it once after you have finished your changes to the data set, there is no reason behind the scenes to call notifyDataSetChanged() for each change.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • I know this thread is old but I am going through a similar problem, what you say is completely correct but what if the user is scrolling the list view while its content has changed. It causes an Illegal state exception then. Would replacing the list altogether before calling `notifyDatasetChanged()` be a good workaround? – rahul.taicho Sep 22 '15 at 06:56