Filter android ListView with predefined filters

随声附和 提交于 2019-12-11 03:09:37

问题


I am pretty new to Android. 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".

My question is 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. Pointers to articles having similar problem statement could also help.


回答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');


来源:https://stackoverflow.com/questions/39470863/filter-android-listview-with-predefined-filters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!