androidsearchviewcustom-adapterandroid-filterablecustom-arrayadapter

Is there an error in my getFilter() method?


I am working on a basic android app that manages contacts. I am using a custom adapter and a custom listview. I have a class ContactItem that represents each contact. Every contact of the listview is a ContactItem instance. When I implemented my search view the app crashes. I followed that answer but each time I write inside the search to filter the listview, the app crashes.

I would appreciate having some supports because I have been trying to find a solution since February. Here is my Adapter :

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;
    }
}

Solution

  • Please check this one

      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();
              }  
          }
         };
    

    Last step,

    @Override
         public Filter getFilter() {
            return myFilter;
        }