如何解决“适配器内容已更改但ListView未收到通知"异常 [英] How to resolve "The content of the adapter has changed but ListView did not receive a notification” exception
问题描述
我发现了很多关于同一主题的问题.但我无法弄清楚我在这里做错了什么.
I found a lot of questions on the same topic. But I am unable to figure out what I am doing wrong here.
异常:适配器的内容已更改,但 ListView 未收到通知.请确保您的适配器的内容不是从后台线程修改的,而是仅从 UI 线程修改的"
Exception: "The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread"
我的 AutoCompleteTextView 有一个 TextWatcher.我正在尝试在文本更改时更新下拉列表.我正在从两个不同的来源获取下拉菜单的数据.而且每一个都在不同的线程中运行.
I have a TextWatcher to my AutoCompleteTextView. I am trying to update the dropdown list when the text changes. I am fetching data for the dropdown from two different sources. And each one of them running 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) {
}
}
};
适配器的处理程序在 textwatcher 旁边.
And the handler for the adapter is next to the textwatcher.
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();
}
}
上面的全部代码都在 Activity 的 OnCreate() 方法中.我并不总是得到例外.它发生在特定的场合.这个问题还没有解决.我在创建适配器后添加了 notifydatasetchanged() 方法.仍然不能解决问题.谁能指出我在这里做错了什么?
The whole code above is inside Activity's OnCreate() method. I don't get the exception always. It happens on specific occasion. This issue is not yet resolved. I have added the notifydatasetchanged() method after the creation of adapter. Still that doesn't solve the problem. Can anyone point out what I am doing wrong here?
推荐答案
通过添加修正错误
suggestionAdapter.notifyDataSetChanged();
on 每当列表更改而不是在 n 次插入后调用它.
on whenever the list is changed instead of calling it after a n number of insertions.
这篇关于如何解决“适配器内容已更改但ListView未收到通知"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!