Víctor Martín
Víctor Martín

Reputation: 3450

How to clean a filtered listview if we haven't results

I do a method to filter a ListView with the text inside of an EditText. My problem is that I put chars inside the edittext, there is a moment which there is no results, but I don't know how to clean the listview.

My textwatcher:

    search_watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (count < before) {
                        // We're deleting char so we need to reset the adapter data
                        adapter.resetData();
                    }

                    adapter.getFilter().filter(s.toString());
...

The adapter is:

private class FarmacoFilter extends Filter {



        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic

            String str_search = constraint.toString();

            if (str_search == null || str_search.length() == 0) {
                // No filter implemented we return all the list
                results.values = farmList;
                results.count = farmList.size();
            }
            else {
                // We perform filtering operation
                List<PojoFarmaco> nFarmList = new ArrayList<PojoFarmaco>();

                for (PojoFarmaco p : farmList) {
                    if (p.getName().toUpperCase().contains(str_search.toUpperCase()) || p.getAmpolla().toUpperCase().contains(str_search.toUpperCase()))
                        nFarmList.add(p);
                }

                results.values = nFarmList;
                results.count = nFarmList.size();

            }
            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                farmList = (List<PojoFarmaco>) results.values;
                notifyDataSetChanged();
            }

        }

    }

Example:

If I write, "GOO", I will find "GOOGLE" and other results, but If I carry on writing more chars like "GOOZZZ", I will find the latest results.

What I need to do? Thanks for your advices.

Upvotes: 2

Views: 74

Answers (2)

V&#237;ctor Mart&#237;n
V&#237;ctor Mart&#237;n

Reputation: 3450

The solution is to edit the publishResults of the getFilter:

@Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0){
                resetData();
                farmList = (List<PojoFarmaco>) results.values;
            }
            else {
                farmList = (List<PojoFarmaco>) results.values;
            }
            notifyDataSetChanged();
        }

It's the same solution as Blackbelt said, but I have the resetData method to revert to original data.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157437

you should keep the original dataset intact. In this moment you are overriding it in this line

farmList = (List<PojoFarmaco>) results.values;

If we call farmListOriginal the original dataSet and farmList a copy of it, you could easily fix it like

 if (results.count == 0)
     farmList = new ArrayList<>(farmListOrig);
 else {
     farmList = (List<PojoFarmaco>) results.values;      
 }
 notifyDataSetChanged();

my assumption is that farmList is an ArrayList

Upvotes: 2

Related Questions