使用 notifyDataSet 在 android 数组适配器中更改时出错

2022-09-02 20:13:39

11-06 19:52:25.958: E/AndroidRuntime(29609): java.lang.IllegalStateException: 适配器的内容已更改,但 ListView 未收到通知。确保适配器的内容不是从后台线程修改的,而是仅从 UI 线程修改的。[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 中的示例,并且它具有相当默认的代码,因此Google代码中似乎存在错误?

该应用程序仅偶尔崩溃,并显示上述错误消息。

protected void publishResults(CharSequence constraint,
        FilterResults results) {

    if (results != null && results.count > 0) {
        notifyDataSetChanged();
    } else {
        notifyDataSetInvalidated();
    }
}

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


答案 1

的方法在后台线程上运行,从该方法可以更改适配器所基于的方法。如果您更改该数据列表,并在这段时间内访问适配器,它将看到它在不知情的情况下发生了某些更改(并且它不会高兴)。您应该避免使用 in 方法,而只需创建一个新的临时列表:FilterperformFiltering()resultListListViewresultListperformFiltering

// 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();
     }
}

答案 2

试试这个(只是一个猜测):

@Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    ArrayList list = autocomplete(constraint.toString());
                    if (list != null) {
                        filterResults.values = list;
                        filterResults.count = list.size();
                    }
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    //change the underlying data immediately before notifying UI                        
                    resultList = (ArrayList)results.values; 
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }};
        return filter;
    }

推荐