我的 getFilter() 方法有错误吗?
Posted
技术标签:
【中文标题】我的 getFilter() 方法有错误吗?【英文标题】:Is there an error in my getFilter() method? 【发布时间】:2020-06-14 09:35:10 【问题描述】:我正在开发一个管理联系人的基本 android 应用程序。我正在使用自定义适配器和自定义列表视图。我有一个代表每个联系人的类 ContactItem。列表视图的每个联系人都是一个 ContactItem 实例。当我实现我的搜索视图时,应用程序崩溃了。我遵循了answer,但每次我在搜索中写入以过滤列表视图时,应用程序都会崩溃。
我希望能得到一些支持,因为自 2 月以来我一直在努力寻找解决方案。 这是我的适配器:
public class CustomAdapter extends ArrayAdapter<ContactItem> implements Filterable
private Context context;
private ArrayList<ContactItem> arrayList;
public CustomAdapter(@NonNull Context context, int resource, ArrayList<ContactItem> arrayList)
super(context, resource, arrayList);
this.context = context;
this.arrayList = arrayList;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.customlist, parent, false);
TextView nom = (TextView) view.findViewById(R.id.nom);
String nomComplet = arrayList.get(position).getPrenom() + " " +arrayList.get(position).getNom();
nom.setText(nomComplet);
return view;
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<ContactItem> templist = new ArrayList<ContactItem>();
//constraint is the text you want to filter your list with
//arraylist is the data set we will filter from
if(constraint != null && arrayList !=null)
int length = arrayList.size();
int i ;
String nomComplet;
Integer id;
String nom, prenom,phone,adresse,email;
for(i=0;i<length;i++)
nomComplet = arrayList.get(i).getNom()+" "+ arrayList.get(i).getPrenom();
if(nomComplet.toUpperCase().contains(constraint.toString().toUpperCase()))
ContactItem item = (ContactItem) arrayList.get(i);
templist.add(item);
filterResults.values = templist;
filterResults.count = templist.size();
else
filterResults.values = arrayList;
filterResults.count = arrayList.size();
return filterResults;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
arrayList = (ArrayList<ContactItem>) results.values;
if(results.count>0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
@NonNull
@Override
public Filter getFilter()
return myFilter;
【问题讨论】:
你能在这里分享你的xml吗 【参考方案1】:请检查这个
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<ContactItem> tempList=new ArrayList<ContactItem>();
//constraint is the result from text you want to filter against.
//objects is your data set you will filter from
if(constraint != null && objects!=null)
int length=objects.size();
int i=0;
while(i<length)
ListTO item=objects.get(i);
//do whatever you wanna do here
//adding result set output array
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
objects = (ArrayList<ContactItem>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
最后一步,
@Override
public Filter getFilter()
return myFilter;
【讨论】:
感谢您的回答,但我看不出您的建议与我的建议有何不同。你能解释一下我做错了什么吗?以上是关于我的 getFilter() 方法有错误吗?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 BaseAdapter 中使用 getFilter() 过滤 ListView
在自定义适配器上使用.getFilter()时出现问题(未正确过滤)(Android)
Android listview 不能用 android 中的 getFilter() 更新