【问题标题】:Add filter to custom listview [duplicate]将过滤器添加到自定义列表视图 [重复]
【发布时间】:2014-09-29 02:48:06
【问题描述】:

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

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


    }

* 现在我可以在这个列表中添加过滤器了吗???

提前致谢

【问题讨论】:

标签: android


【解决方案1】:

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

在您的适配器中

  1. 您需要在您的MyCustomAdapter 中实现Filterable
  2. 使用Filter 类在覆盖getFilter 中添加逻辑。

在您的列表视图中

  1. setTextFilterEnabled(true);

在您的 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;
}
});

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2011-11-12
    • 2014-12-08
    相关资源
    最近更新 更多