How to resolve "Adapter contents changed but ListView exception did not receive notification"

I found many questions on one topic. But I can’t understand what I am doing wrong here.

Exception: "The contents of the adapter changed, but the ListView did not receive a notification. Make sure that the contents of your adapter are not changed from the background stream, but only from the user interface stream."

I have a TextWatcher for my autocomplete. I am trying to update a dropdown when changing text. I am retrieving data for a drop-down list from two different sources. And each of them works inside different threads.

mTextWatcher = new TextWatcher() { public void afterTextChanged(Editable editable) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { suggestionNewText = s.toString(); list = new ArrayList<Map<String, String>>(); runOnUiThread(new Runnable() { @Override public void run() { //data processing list.add(data); handler.sendEmptyMessage(1); } }); Thread webAutoComplete = new Thread(new Runnable() { @Override public void run() { //data process list.add(data); handler.sendEmptyMessage(1); } }); try { webAutoComplete.start(); } catch (NullPointerException e) { } catch (IllegalStateException e) { } } }; 

And the adapter handler is next to the textwatcher element.

  handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: { ((Activity) CONTEXT).runOnUiThread(new Runnable() { public void run() { try { if (list != null) suggestionAdapter = new UrlSuggestionAdapter( CONTEXT, list, R.layout.two_line_autocomplete, new String[] { "title", "url" }, new int[] { R.id.title, R.id.url }); if (list != null) { refreshAdapter(list); getUrl.setAdapter(suggestionAdapter); } } catch (Exception e) { } } }); break; } case 2: { break; } } } }; public synchronized void refreshAdapter(List<Map<String, String>> items) { list = items; if (list != null) { suggestionAdapter.notifyDataSetChanged(); } } 

All the code above is inside the Activity OnCreate () method. I do not get an exception always. This happens on a specific occasion. This problem has not yet been resolved. After creating the adapter, I added the notifydatasetchanged () method. However, this does not solve the problem. Can someone point out what I'm doing wrong here?

+6
source share
1 answer

Bug fixed by adding

 suggestionAdapter.notifyDataSetChanged(); 

each time the list is changed, instead of calling it after the n number of inserts.

+4
source

Source: https://habr.com/ru/post/954564/


All Articles