Listview filter adapter.getFilter().filter(by more than one value)

亡梦爱人 提交于 2019-12-11 01:03:12

问题


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;
            }
        });

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)?

EDIT: this is my filter

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();

    }
}

回答1:


You can check my reference for your question.

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;
 }

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.




回答2:


You cannot change API of the framework class. There is no method on the Filter class that takes multiple constraint arguments. If you want to keep using Filter, you can only combine your arguments in a way that you can separate them again inside of the filter() method, e.g. by joining them into a comma-separated string and then splitting the string.

Alternatively, you don't need to use the Filter class, you can make your own class that can take (for instance) a List<String> and does the filtering you want, and then set the filtered results in your adapter. The only difficult part is now you must handle the logic for doing that work in the background, canceling the work if another filter call happens while it's in progress, etc.




回答3:


 if (arrayList.contains(query))
        {
            arrayAdapter.getFilter().filter(query);
        }
        else
        {
            Toast.makeText(MainActivity.this, "not found", Toast.LENGTH_SHORT).show();
        }
        return false;

    }


来源:https://stackoverflow.com/questions/36825456/listview-filter-adapter-getfilter-filterby-more-than-one-value

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