2

I want to implement predefined filters for a ListView. My ListView will contain numbers and there would be a Filter icon which on click should show options like "Show odd", "Show even" and "Show all".

enter image description here

How to display a popup dialog on click of the Filter icon? If that is achieved using a simple popup dialog, then how do I filter the ListView with the option selected? I tried searching Android forums but they speak mainly about text filters.

1
  • use this generic adapter Commented Sep 13, 2016 at 14:31

1 Answer 1

1

You could implement a Filter like so:

class MyFilter extends Filter {

    private final MyAdapter myAdapter;

    public MyFilter(MyAdapter myAdapter) {
        this.myAdapter = myAdapter;
    }

    @Override
    protected Filter.FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint == null || constraint.length() == 0) {
    // unfiltered: show all
            results.values = myAdapter.getOriginalList();
            results.count = myAdapter.getOriginalList().size();
        } else {
    // filtered
            List<Integer> newWorkingList = new ArrayList<>();
            if (constraint.equals('1')) {
    // odd
                for (Integer integer : myAdapter.getOriginalList()) {
                    if (integer % 2 == 1) {
                            newWorkingList.add(integer);
                    }
                }
            } else if (constraint.equals('2')) {
    // even
                for (Integer integer : myAdapter.getOriginalList()) {
                    if (integer % 2 == 0) {
                        newWorkingList.add(integer);
                    }
                }
            }
            results.values = newWorkingList;
            results.count = newWorkingList.size();
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        myAdapter.setFilteredList((List<String>) results.values);
        if (results.count == 0) {
            myAdapter.notifyDataSetInvalidated();
        } else {
            myAdapter.notifyDataSetChanged();
        }
    }
}

In your adapter class you have to make changes similar to these:

public class MyAdapter extends ArrayAdapter<Integer> implements Filterable

    private MyFilter MyFilter;

    @Override
    public Filter getFilter() {
        if (myFilter == null) {
            myFilter = new myFilter(this);
        }
        return myFilter;
    }

And you have to add a setter for your original list.

And, finally, in the listener of your popup dialog you have to add these lines depending on the user's choice:

myAdapter.getFilter().filter(null);
myAdapter.getFilter().filter('1');
myAdapter.getFilter().filter('2');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.