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

Upv*_*ote 18 java android android-listview

11-06 19:52:25.958:E/AndroidRuntime(29609):java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知.确保不从后台线程修改适配器的内容,而只是从UI线程修改.[在ListView(-1,类android.widget.ListPopupWindow $ DropDownListView)中使用Adapter(类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();
    }
}
Run Code Online (Sandbox Code Playgroud)

活动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);
        }
}
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决这一问题?我在android 4.3上.

Luk*_*rog 34

该Filter的performFiltering()方法在后台线程上运行,并从该方法你改变了resultList上您的适配器的基础.如果您更改了该数据列表,并且在那段时间内ListView访问适配器,它会在不知情的情况下看到某些内容发生了变化(并且不会感到高兴).您应该避免resultList在performFiltering方法中使用,只需创建一个新的临时列表:

// 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();
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 优秀的家伙!非常非常veryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy帮助.难以置信...... (2认同)