我已经实现了自定义适配器的搜索功能,但仍然无法正常工作,如果有任何错误,我已经发布了我的代码。
java代码
@Override
public Filter getFilter() {
/**
* A filter object which will filter message key
* */
return filter;
}
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mEventUtil = (List<Event>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<Event>(mEventUtil); // mOriginalValues
}
// if (mListItem == null) {
// mListItem = new ArrayList<String>();
// for (Event message : mOriginalValues) {
// mListItem.add(message.getEvent_Title());
// }
// }
/**
*
* If constraint(CharSequence that is received) is null returns
* the mOriginalValues(Original) values else does the Filtering
* and returns FilteredArrList(Filtered)
*
**/
if (constraint == null || constraint.length() == 0) {
/*
* CONTRACT FOR IMPLEMENTING FILTER : set the Original
* values to result which will be returned for publishing
*/
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
/* Do the filtering */
constraint = constraint.toString().toLowerCase();
List<Event> FilteredArrList = new ArrayList<Event>(mOriginalValues.size());
for (int i = 0; i < mOriginalValues.size(); i++) {
Event event = mOriginalValues.get(i);
if (event.getEvent_Title().toLowerCase().contains(constraint.toString())) {
FilteredArrList.add(event);
}else{
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
答案 0 :(得分:0)
这是我最常见的实施方式:
@Override
public Filter getFilter() {
if (ownfilter == null)
ownfilter = new CustomFilter();
return ownfilter;
}
基本上,ownfilter
是CustomFilter
类的实例,它是Filter
类的扩展。在Filter
扩展名中,您必须覆盖以下方法:
protected FilterResults performFiltering(CharSequence)
:您可以定义如何对传递给方法的CharSequence
执行过滤。您必须返回一个FilterResults
对象,您必须为其设置两个字段:values
,这是过滤器匹配的List
和count
,这是基本上是匹配器的数量(List.size())。
protected void publishResults(CharSequence, FilterResults)
:您将从FilterResults
传递performFiltering
,您需要做的就是notifyDataSetChanged()
count > 0
notifyDataSetInvalidated()
1}}或{{1}}否则。
就是这样。
答案 1 :(得分:0)
像这样使用..它对我来说很完美
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final ArrayList<HashMap<String, String>> list = all_details;
int count = list.size();
final ArrayList<HashMap<String, String>> newlist = new ArrayList<HashMap<String, String>>();
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = list.get(i).get("prodname");
if (filterableString.toString().toLowerCase().contains(filterString)) {
newlist.add(list.get(i));
}
}
results.values = newlist;
results.count = newlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null) {
if (results.count > 0) {
pt_details = new ArrayList<HashMap<String,String>>((ArrayList<HashMap<String, String>>) results.values) ;
} else {
pt_details.clear();
//pt_details = all_details;
}
} else {
pt_details = all_details;
}
notifyDataSetChanged();
}
}