1

I have getFilter in my recyclerView adapter class. It is not working properly if I search a name that starts with 'A' there is no change in the list results. Suppose I search a name that starts with 'S', the name will come to the top of the list. Also I try the same thing with phone number and pincode. It doesn't show anything. This is What I am trying:

 EditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if(adapter!=null){
                adapter.getFilter().filter(s);
            }
        }
    });

This above code I used to send the characters to get filter method:

     @Override
public Filter getFilter() {
     return new Filter() {
         @Override
         protected FilterResults performFiltering(CharSequence constraint) {
             String charString = constraint.toString().toLowerCase();
             if(charString.isEmpty()){
                 mDataFiltered = mData;
             }
             else{

                 List<Guru> filteredList  = new ArrayList<Guru>();
                 for(Guru row:mData){
                     if(row.getName().toLowerCase().contains(charString) ||
                        row.getMobile().contains(charString) || row.getOfficePincode().contains(charString)|| row.getHomePincode().contains(charString) ){
                         filteredList.add(row);
                     }
                 }
                 mDataFiltered = filteredList;

             }
             FilterResults filterResults = new FilterResults();
             filterResults.values = mDataFiltered;
             return filterResults;
         }

         @Override
         protected void publishResults(CharSequence constraint, FilterResults results) {
             if(results.values!=null){
                 mDataFiltered = (List<Guru>) results.values;
                 notifyDataSetChanged();
             }
         }
     };

Here I am doing the filter function. If you know the mistake just ping me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • do not implement `Filterable` again and again every time you need to filter some adapter: instead use adapter that already implements `Filterable` interface - like [this](https://gist.github.com/pskink/cd3bbdd742b5b1905a790c76831b5d85) for example - all you need to do is to override `matches()` method – pskink Dec 14 '18 at 07:22
  • If you are trying to achieve seach functionality you can see this . Its much more simpler https://stackoverflow.com/questions/27378981/how-to-use-searchview-in-toolbar-android/49064027#49064027 – Rohit Singh Dec 14 '18 at 08:24
  • hey sorry for late response it is very helpful for me as a beginner.i felt overwhelmed by your response guys – KarthikeyanAndroid Dec 14 '18 at 19:24
  • @pskink yeah i got it – KarthikeyanAndroid Dec 14 '18 at 19:24

0 Answers0