Listview过滤器adapter

Listview过滤器adapter

本文介绍了Listview过滤器adapter.getFilter().filter(多于一个值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在android中创建了一个列表视图,并且已经通过使用searchview和charsequence作为像这样的参数来实现搜索过滤器

I have created a list view in android and I already implement search filter by using searchview and charsequence as parameter like this

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                adapter.getFilter().filter(query);
                return false;
            }
        });

现在如何获得不止一个值的过滤器结果? adapter.getFilter().filter("example" || "example" || example);有可能这样做吗?或者也许我可以使用adapter.getFilter().filter(array here)?

now how can I get the filter result by more than one value? adapter.getFilter().filter("example" || "example" || example);is it possible to do that? or maybe can I using adapter.getFilter().filter(array here)?

这是我的过滤器

public class CustomFilter extends Filter {

    List<Event> filterList;
    EventAdapter adapter;

    public CustomFilter(List<Event> filterList, EventAdapter adapter) {
        this.filterList = filterList;
        this.adapter = adapter;
    }

    //FILTERING
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        //RESULTS
        FilterResults results=new FilterResults();

        //VALIDATION
        if(constraint != null && constraint.length()>0)
        {

            //CHANGE TO UPPER FOR CONSISTENCY
            constraint=constraint.toString().toUpperCase();

            ArrayList<Event> filteredEvent=new ArrayList<>();

            //LOOP THRU FILTER LIST
            for(int i=0;i<filterList.size();i++)
            {
                //FILTER (tinggal mainin logic aja mau filter berdasarkan apa nya )
                if(filterList.get(i).getJudul().toUpperCase().contains(constraint)|| filterList.get(i).getDeskripsi().toUpperCase().contains(constraint))
                {
                    filteredEvent.add(filterList.get(i));
                }
            }

            results.count=filteredEvent.size();
            results.values=filteredEvent;
        }else
        {
            results.count=filterList.size();
            results.values=filterList;
        }

        return results;
    }


    //PUBLISH RESULTS

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

        adapter.eventes= (List<Event>) results.values;
        adapter.notifyDataSetChanged();

    }
}

推荐答案

您可以查看我的参考文献来解决您的问题.

You can check my reference for your question.

我用一个String来实现这一点,其中包括两个不同过滤器的值,并在String中的两个值之间添加一个分隔符(在本例中为-").

i implement this with one String that include the values for two different filters , and add a split mark (in this case : "-") between the two values in the String.

MainActivity.class

            String lat = latitude.getText().toString();
            String lon = longitude.getText().toString();
            //join the two strings and add a split mark '-'
            String join = lat + "-" + lon;

            mca.getFilter().filter(join); //mca is my cursorAdapter
            mca.notifyDataSetChanged();

            mca.setFilterQueryProvider(new FilterQueryProvider() {
                @Override
                public Cursor runQuery(CharSequence constraint) {
                    String value = constraint.toString();
                    return getFilterResults(value);
                }

            });

getFilterResult(String filterValue)

 public Cursor getFilterResults(String filterValue) {

    Cursor c = getLoactionTimeEntry(); //some default init
    String lat = "";
    String lon = "";
    String[] splitFilterValue = filterValue.split("-");

    if(filterValue.charAt(0) == '-') {
        lon = splitFilterValue[1];
        c = getLongitudeFilter(lon);
    }
    else if(splitFilterValue.length == 2) {
        lat = splitFilterValue[0];
        lon = splitFilterValue[1];
        c = getLongtitudeLatitudeFilter(lat,lon);
    }
    else {
        lat = splitFilterValue[0];
        c = getLatitudeFilter(lat);
    }

    return c;
 }

在我的过滤器方法中,我用功能split()分割了String并将值存储回单独的字符串变量.功能:getLongitudeFilter(lon)getLongtitudeLatitudeFilter(lat,lon)getLatitudeFilter(lat)是我的函数,在这种情况下,该函数返回一些Cursor以便从数据库中获取所需的信息.

in my filters method i split the String with the function split()and store the values back to separate string's variables.the functions: getLongitudeFilter(lon) , getLongtitudeLatitudeFilter(lat,lon), getLatitudeFilter(lat)is my functions that return some Cursor to get what i need from the DataBase in this case.

这篇关于Listview过滤器adapter.getFilter().filter(多于一个值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:14