java - 在 android 数组适配器中使用 notifyDataSetChanged 时出错

标签 java android android-listview

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 上。

最佳答案

FilterperformFiltering() 方法在后台线程上运行,您可以通过该方法更改您的 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/

相关文章:

java - 如何在 TableView (javaFX 8) 的 TableColumn 中显示 "single bar chart"?

android - 尝试使用 react-native-dialogflow 分配给只读属性 - 错误

java - 尝试使用一个 JSONArray 解析 JSONObject 时出现空 JSON 响应

android - 上下文操作栏不覆盖我的工具栏

java - EclipseLink JPA 2.1 用户提供的连接

java - 从java获取不正确的子图像

java - 在 Java 中使用 Haxe 代码

android - 如何仅在退出应用程序时结束服务?

android - 将视频添加到应用程序或youtube API?

android - 使listview的RelativeLayout在每个项目上对齐