11-06 19:52:25.958: E/AndroidRuntime(29609): java.lang.IllegalStateException: 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. [in ListView(-1, class android.widget.ListPopupWindow$DropDownListView) with Adapter(class com.example.parkfoxxlight_android.PlacesAutoCompleteAdapter)]
完整日志:http://pastebin.com/Hx7k28Rm
适配器的完整代码:http://pastebin.com/TfH1bXE3我正在使用 https://developers.google.com/places/training/autocomplete-android 中的示例而且它有相当的默认代码,所以谷歌代码似乎有一个错误?
应用程序仅有时会崩溃并显示上述错误消息。
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
Activity http://pastebin.com/FYzYtvXY :
public class CityActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.city);
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autocomplete_city);
PlacesAutoCompleteAdapter ad = new PlacesAutoCompleteAdapter(this);
ProgressBar b = (ProgressBar)findViewById(R.id.progressBar1);
ad.setLoadingIndicator(b);
autoCompView.setAdapter(ad);
}
}
任何想法如何解决这个问题?我在安卓 4.3 上。
最佳答案
Filter
的 performFiltering()
方法在后台线程上运行,您可以通过该方法更改您的 resultList
适配器是基于。如果您更改该数据列表并且在那时 ListView
访问适配器,它将看到某些东西在它不知情的情况下发生了变化(并且它不会高兴)。您应该避免在 performFiltering
方法中使用 resultList
并简单地创建一个新的临时列表:
// in the performFiltering method which runs on a background thread:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<String> queryResults;
if (constraint != null && constraint.length() > 0) {
queryResults = autocomplete(constraint);
} else {
queryResults = new ArrayList<String>(); // empty list/no suggestions showing if there's no valid constraint
}
filterResults.values = queryResults;
filterResults.count = queryResults.size();
return filterResults; // ## Heading ##
}
private List<String> autocomplete(String input) {
// don't use the here the resultList List on which the adapter is based!
// some custom code to get items from http connection
ArrayList<String> queryResults = new ArrayList<String>(); // new list
queryResults.add("Some String");
return queryResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// update the data with the new set of suggestions
resultList = (ArrayList<String>)results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
关于java - 在 android 数组适配器中使用 notifyDataSetChanged 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19820736/