参见英文答案 >
Android, ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification” 24个
我在同一主题上发现了很多问题.但我无法弄清楚我在这里做错了什么.
例外:
“适配器的内容已更改,但ListView未收到通知.请确保未从后台线程修改适配器的内容,但只能从UI线程修改”
我有一个TextWatcher到我的AutoCompleteTextView.我试图在文本更改时更新下拉列表.我从两个不同的来源获取下拉列表的数据.并且每个人都在不同的线程内运行.
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>();
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旁边.
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> items) {
list = items;
if (list != null) {
suggestionAdapter.notifyDataSetChanged();
}
}
上面的整个代码都在Activity的OnCreate()方法中.我总是不会得到例外.它发生在特定场合.此问题尚未解决.我在创建适配器后添加了notifydatasetchanged()方法.仍然没有解决问题.谁能指出我在这里做错了什么?