我正在开发一个管理联系人的基本android应用程序。我正在使用自定义适配器和自定义列表视图。我有一个类ContactItem,它代表每个联系人。列表视图的每个联系人都是一个ContactItem实例。当我实现我的搜索视图时,应用程序崩溃了。我遵循了这个答案,但每次我在搜索中写入内容以过滤列表视图时,应用程序都会崩溃。
如果能得到一些支持,我将不胜感激,因为自二月份以来,我一直在努力寻找解决方案。这是我的适配器:
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;
}
}
请检查这个
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;
}