Java 如何解决“适配器内容已更改但ListView未收到通知”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18999493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve "The content of the adapter has changed but ListView did not receive a notification” exception
提问by intrepidkarthi
I found a lot of questions on the same topic. But I am unable to figure out what I am doing wrong here.
我发现了很多关于同一主题的问题。但我无法弄清楚我在这里做错了什么。
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"
异常:“适配器的内容已更改,但 ListView 未收到通知。请确保适配器的内容不是从后台线程修改的,而是仅从 UI 线程修改的”
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.
我的 AutoCompleteTextView 有一个 TextWatcher。我正在尝试在文本更改时更新下拉列表。我正在从两个不同的来源获取下拉菜单的数据。它们中的每一个都在不同的线程中运行。
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 handler for the adapter is next to the textwatcher.
适配器的处理程序位于 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();
}
}
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?
上面的整个代码都在 Activity 的 OnCreate() 方法中。我并不总是得到例外。它发生在特定的场合。这个问题还没有解决。我在创建适配器后添加了 notifydatasetchanged() 方法。仍然不能解决问题。谁能指出我在这里做错了什么?
采纳答案by intrepidkarthi
Fixed the error by adding
通过添加修复了错误
suggestionAdapter.notifyDataSetChanged();
on whenever the list is changed instead of calling it after a n number of insertions.
每当列表更改而不是在多次插入后调用它。