添加过滤器到自定义列表视图

时间:2014-08-06 08:39:14

标签: android

我想将过滤器添加到自定义列表视图中,我尝试使用:

MainActivity.this.adapter.getFilter().filter(cs);

但这不能正常工作;

我的代码是:

inputSearch =(EditText)findViewById(R.id.editTextContact);
//get list of items from database.
ArrayList<String> listOfContactToSaveInDataBase = new ArrayList<String>();
SQLiteDatabase db;
db = openOrCreateDatabase("Saftey.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
Cursor cur = db.query("Contact", null, null, null, null, null, null);
cur.moveToFirst();
listOfContactToSaveInDataBase.clear();
while (cur.isAfterLast() == false) {
    listOfContactToSaveInDataBase.add(cur.getString(2)); // number
    listOfContactToSaveInDataBase.add(cur.getString(1)); // name
    cur.moveToNext();
}
cur.close();
customAdapterToDisplayList = new MyCustomAdapter(this, R.layout.contact_info, countryList);
ListView listView = (ListView) findViewById(R.id.list1);
listView.setAdapter(customAdapterToDisplayList);

//custom adapter for contact list
    private class MyCustomAdapter extends ArrayAdapter<addContact> {
        private ArrayList<addContact> countryList;



public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<addContact> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<addContact>();
            this.countryList.addAll(countryList);
        }

        private class ViewHolder {
            TextView code;
            CheckBox name;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.contact_info, null);

                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
                convertView.setTag(holder);
                holder.name.setOnClickListener(new View.OnClickListener() {
                    @SuppressWarnings("deprecation")
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        addContact country = (addContact) cb.getTag();

                        if (cb.isChecked() == true) {
                            //add to list
                        } else {
                            //remove from list
                        }
                        country.setSelected(cb.isChecked());
                    }
                });
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            addContact country = countryList.get(position);
            holder.code.setText("");
            holder.name.setText(country.getName() + " \n" + country.getCode());
            holder.name.setChecked(country.isSelected());
            holder.name.setTag(country);

            return convertView;
        }


    }

* 现在我可以在此列表中添加过滤器???

提前致谢

1 个答案:

答案 0 :(得分:1)

如果我仔细理解这个问题。我认为这一步就足够了。

在您的适配器中

  1. 您需要在Filterable
  2. 中实施MyCustomAdapter
  3. 使用getFilter类覆盖Filter添加逻辑。
  4. 在Listview中

    1. setTextFilterEnabled(true);
    2. 在您的SearchView中

      setOnQueryTextListener(new OnQueryTextListener(){
          @Override
      public boolean onQueryTextSubmit(String query) {
          // TODO Auto-generated method stub
      
          adapter.getFilter().filter(query);
      
          return true;
      
      
      }
      
      @Override
      public boolean onQueryTextChange(String newText) {
          // TODO Auto-generated method stub
          adapter.getFilter().filter(newText);
          return true;
      }
      });
      

      希望这会有所帮助:)