SimpleAdapter自定义过滤器(SimpleAdapter custom filter)

我正在尝试在SimpleAdapter上运行自定义过滤器,并且似乎在最后一道障碍。 在调试代码时,我可以在publishResults方法的itemsFiltered中看到生成的过滤的ArrayList,但始终显示完整的列表。

如何使适配器在过滤结果列表中工作而不是完整的未过滤列表?

代码是:

private class TextCharFilter extends Filter{ @Override protected FilterResults performFiltering(CharSequence constraint) { // convert search string to lower case - the filtering is not case sensitive constraint = constraint.toString().toLowerCase(); // define the result object FilterResults result = new FilterResults(); // define a place to hole the items that pass filtering List<HashMap<String, String>> filteredItems = new ArrayList<HashMap<String,String>>(); // loop through the original list and any items that pass filtering are added to the "filtered" list if(constraint != null && constraint.toString().length() > 0) { for(int i = 0; i < items.size(); i++) { HashMap<String, String> tmp = items.get(i); String candidate = tmp.get("PT").toLowerCase(); if(candidate.contains(constraint) ) { filteredItems.add(tmp); } } // set the result to the "filtered" list. result.count = filteredItems.size(); result.values = filteredItems; } else { // if nothing to filter on - then the result is the complete input set synchronized(this) { result.values = items; result.count = items.size(); } } return result; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { ArrayList<HashMap<String, String>> tmp = (ArrayList<HashMap<String, String>>)results.values; itemsFiltered = new ArrayList<HashMap<String,String>>(); for (int i = 0; i < tmp.size(); i++){ itemsFiltered.add(tmp.get(i)); } notifyDataSetChanged(); notifyDataSetInvalidated(); } }

I'm trying to get a custom filter on a SimpleAdapter working and seem to be falling at the last hurdle. When debugging the code I can see the resulting filtered ArrayList in itemsFiltered in the publishResults method, but the complete list is always shown.

How do I get the adapter to work of the filtered results list not the complete unfiltered list?

Code is :

private class TextCharFilter extends Filter{ @Override protected FilterResults performFiltering(CharSequence constraint) { // convert search string to lower case - the filtering is not case sensitive constraint = constraint.toString().toLowerCase(); // define the result object FilterResults result = new FilterResults(); // define a place to hole the items that pass filtering List<HashMap<String, String>> filteredItems = new ArrayList<HashMap<String,String>>(); // loop through the original list and any items that pass filtering are added to the "filtered" list if(constraint != null && constraint.toString().length() > 0) { for(int i = 0; i < items.size(); i++) { HashMap<String, String> tmp = items.get(i); String candidate = tmp.get("PT").toLowerCase(); if(candidate.contains(constraint) ) { filteredItems.add(tmp); } } // set the result to the "filtered" list. result.count = filteredItems.size(); result.values = filteredItems; } else { // if nothing to filter on - then the result is the complete input set synchronized(this) { result.values = items; result.count = items.size(); } } return result; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { ArrayList<HashMap<String, String>> tmp = (ArrayList<HashMap<String, String>>)results.values; itemsFiltered = new ArrayList<HashMap<String,String>>(); for (int i = 0; i < tmp.size(); i++){ itemsFiltered.add(tmp.get(i)); } notifyDataSetChanged(); notifyDataSetInvalidated(); } }

最满意答案

看起来您正在向在publishResults()中创建的arraylist中添加项目。 这些项永远不会添加到实际的适配器中。 你应该清除publishResults()中的适配器,然后重新添加项目。或者只是从过滤器列表中创建一个新的适配器,并将其设置为listview的适配器。

it looks like you are adding items to an arraylist that is created in publishResults(). the items are never added to the actual adapter. you should clear the adapter in publishResults() and then add the items back in. or just create a new adapter from the filter list and set that as your listview's adapter.

更多推荐