0

I have written a customer filter inside my adapter to allow a user to search by a customer's name. I followed the answers provided on this question, specifically the answer with 35 up-votes, as the selected answer modifies the original list and causes errors. The filtering works correctly, but if you press backspace after searching for a customer name, the results do not update. Here is my filter method, thanks for any help beforehand.

@Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                ArrayList<CustomerView> suggestions = new ArrayList<>();
                if (constraint != null) {
                    suggestions.clear();
                    for (CustomerView customer : customers) {
                        if (customer.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                            suggestions.add(customer);
                        }
                    }

                    results.values = suggestions;
                    results.count = suggestions.size();
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                clear();
                if (results != null && results.count > 0) {
                    addAll((ArrayList<CustomerView>) results.values);
                } else {
                    addAll(customers);
                }
                notifyDataSetChanged();
            }
        };
    }
Community
  • 1
  • 1
wizloc
  • 2,202
  • 4
  • 28
  • 54

1 Answers1

0

Why don't you create your custom search mechanism to filter the results in your Activity or Fragment instead of Adapter.

If you are using an EditText then add "TextWatcher" to your edit text. like the link given below:

How to use the TextWatcher class in Android?

And if you are using Searchview then simply add "OnqueryTextChangeListener" to your serchview. like the link iven below:

how to get the events of searchview in android

And Simply add some mechanism for filtration according to your requirements like the sample code given below:

 @Override
        public boolean onQueryTextChange(String s) {

            textlength = s.length();

            arr_sort.clear();


            for (int i = 0; i < tunesList.size(); i++) {


                String heading = (String) tunesList.get(i).
                        getTuneName();
                String[] words = heading.split("\\s+");

                for (String item : words) {

                    Log.e("Words" , item);
                    if (s.length() <= (int) item.length()) {

                        if (s.equalsIgnoreCase((String) item.subSequence(0, s.length())))


                        {
                            RowItemMain cObj = tunesList.get(i);

                            if(arr_sort.contains(cObj))
                            {

                            }else{
                                arr_sort.add(cObj);
                            }



                        }
                    }
                }


            }




            try
            {


                AdapterMain ivAdp = new AdapterMain(MainActivity.this, arr_sort);
                lvSounds.setAdapter(ivAdp);

            } catch (Exception e) {


                e.printStackTrace();
            }


            return true;
        }
Community
  • 1
  • 1
Zohaib Hassan
  • 984
  • 2
  • 7
  • 11