如何在Adapter或Listview上应用多个过滤器?

时间:2016-09-30 09:35:51

标签: android listview adaptor listview-filter

我有ListView和Bus信息列表,我想在Adapter或ListView中应用多个过滤器,我已经在ListView上完成了实时搜索过滤器,

这是我的活动:

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

}

以下是我的屏幕视图 enter image description here

当我在文本视图上搜索其搜索时,我想要过滤区域和人口的ListView。

3 个答案:

答案 0 :(得分:0)

在Listview中应用多个过滤器,并在ListView中使用多重排序,请尝试以下链接:

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

enter image description here

答案 1 :(得分:0)

您可以在类似于下面的performFiltering(CharSequence约束)方法中添加条件

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

答案 2 :(得分:0)

   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

        }
    });

并且:

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