How to apply multiple Filters on an Adapter Or Listview?

3.4k Views Asked by At

I have ListView with list of Bus information, I want to apply the multiple filter on Adapter or in ListView, I have already done the live search filter on ListView,

Here is my Activity:

public class FilterListActivity extends Activity implements TextWatcher {

    private static List<Country> countries = Storage.getItems();
    private EditText editTextFilter;
    private ListView listViewCountries;
    private CountryListAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new CountryListAdapter(this, countries);

        editTextFilter = (EditText)findViewById(R.id.editTextFilter);
        editTextFilter.addTextChangedListener(this);

        listViewCountries = (ListView)findViewById(R.id.listViewCountries);
        listViewCountries.setAdapter(adapter);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable s) {}

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

}

and below is my screen view enter image description here

When i search on text view its searches very well but i want to filter ListView with area and population also.

3

There are 3 best solutions below

0
Apoorv Bambarde On

Apply Multiple Filter in Listview and also use the multi sorting in ListView, try this link:

https://github.com/apurv3039/filter_listview/tree/master

enter image description here

0
Mohd Qasim On

you can add condition in performFiltering(CharSequence constraint) method like below

ListAdapter implements Filterable{
 @Override
public Filter getFilter() {
    if (valueFilter == null) {
        valueFilter = new ValueFilter();
    }
    return valueFilter;
}
private class ValueFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if (constraint != null && constraint.length() > 0) {
            List<DrugsListPojo> filterList = new ArrayList<>();
            for (int i = 0; i < mdataListFilterList.size(); i++) {
                if ((mdataListFilterList.get(i).getName().toUpperCase()).contains(constraint.toString().toUpperCase())) {
                    filterList.add(mdataListFilterList.get(i));
                }else  if ((mdataListFilterList.get(i).getDrugTypeName().toUpperCase()).contains(constraint.toString().toUpperCase())) {
                    filterList.add(mdataListFilterList.get(i));
                }
            }
            results.count = filterList.size();
            results.values = filterList;
        } else {
            results.count = mdataListFilterList.size();
            results.values = mdataListFilterList;
        }
        return results;

    }

    @Override
    protected void publishResults(CharSequence constraint,
                                  FilterResults results) {
        dataList = (List<DrugsListPojo>) results.values;
        notifyDataSetChanged();
    }
}
1
Ak.Ha On
   etSearchbox=(EditText)findViewById(R.id.etSearchbox);
    etSearchbox.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
           // Dar_List.this.adapter.getFilter().filter(arg0);
            List<DarParClass> Dar_List_New= new ArrayList();
            for (DarParClass var : Dar_List)
            {
                if (var.Date1.contains(arg0)){//you can add multiple Filters here with '||'
                    Dar_List_New.add(var);
                }
            }
            adapter = new Dar_List_ListViewAdapter(Dar_List_New, getBaseContext());
            listView.setAdapter(adapter);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });

And:

public class DarParClass {
  String KodD,Kodmh,Ham,Rate,RateM,Price1,Distin1,Pricesum1,Date1,Refe1_n;

  public DarParClass(String KodD, String Kodmh, String Ham, String Rate, String RateM, String Price1, String Distin1, String Pricesum1, String Date1, String Refe1_n) {
    this.KodD = KodD;

    this.Kodmh = Kodmh;
    this.Ham = Ham;
    this.Rate = Rate;
    this.RateM = RateM;
    this.Price1 = Price1;
    this.Distin1 = Distin1;
    this.Pricesum1 = Pricesum1;
    this.Date1 = Date1;
    this.Refe1_n = Refe1_n;
  }
  public String getKodD() {return KodD;}

  public String getKodmh() {return Kodmh;}
  public String getHam() {return Ham;}
  public String getRate() {return Rate;}
  public String getRateM() {return RateM;}
  public String getPrice1() {return Price1;}
  public String getDistin1() {return Distin1;}
  public String getPricesum1() {return Pricesum1;}
  public String getDate1() {return Date1;}
  public String getRefe1_n() {return Refe1_n;}
}