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() {
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?