我已经为自定义适配器实现了搜索功能,但仍然无法正常工作,如果有任何错误,我已经发布了我的代码,请纠正我。
爪哇代码
@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;
}
};