您可以创建自己的 FilterAdapter 并带来自己的逻辑,例如,这个处理带有两个字符串的对象并管理过滤器:
public class TwoWordsFilter extends Filter {
ArrayList<BranchData> branchDatas = new ArrayList<BranchData>();
private BranchDataAdapter branchDataAdapter;
public TwoWordsFilter(ArrayList<BranchData> branchDatas,BranchDataAdapter branchDataAdapter) {
this.branchDatas = branchDatas;
this.branchDataAdapter=branchDataAdapter;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint==null || constraint.length()==0){
results.values=branchDatas;
results.count=branchDatas.size();
}else {
List<BranchData> mBranchDatas=new ArrayList<BranchData>();
for (BranchData branchData : branchDatas) {
if (branchData.getBranch_name().trim().toLowerCase().startsWith(constraint.toString().trim().toLowerCase()) ||
branchData.getCity().trim().toLowerCase().startsWith(constraint.toString().trim().toLowerCase())){
mBranchDatas.add(branchData);
}
}
results.values=mBranchDatas;
results.count=mBranchDatas.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0)
branchDataAdapter.notifyDataSetInvalidated();
else {
branchDataAdapter.setBranchDatas((ArrayList<BranchData>) results.values);
branchDataAdapter.notifyDataSetChanged();
}
}
}
在你的适配器中放这个:
@Override
public Filter getFilter() {
if (twoWordsFilter==null){
twoWordsFilter=new TwoWordsFilter(branchDatas,this);
}
return twoWordsFilter;
}