我正在尝试向SimpleAdapter添加自定义过滤器,我在publishResults方法中遇到错误。
代码是
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) {
itemsFiltered = (List<HashMap<String, String>>)results.values;
notifyDataSetChanged();
clear();
for (int i = 0; i < itemsFiltered.size(); i++){
add(itemsFiltered.get(i));
}
notifyDataSetInvalidated();
}
}
错误是找不到方法clear()和add(itemsFiltered.get(i)) - 我哪里错了?该代码基于已发布的示例。