该
Filter的
performFiltering()方法在后台线程,并从该方法运行你改变了
resultList上您的适配器的基础。如果您更改了该数据列表,并且在那段时间
ListView访问了适配器,它将看到某些内容在其不知情的情况下发生了更改(并且不会感到高兴)。您应该避免使用
resultListin
performFiltering方法,而只需创建一个新的临时列表:
// in the performFiltering method which runs on a background thread:@Overrideprotected 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 pre to get items from http connection ArrayList<String> queryResults = new ArrayList<String>(); // new list queryResults.add("Some String"); return queryResults;}@Overrideprotected 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(); }}


