【问题标题】:implement search filter on listview filled from database在从数据库填充的列表视图上实现搜索过滤器
【发布时间】:2012-11-28 15:36:38
【问题描述】:

我正在从Database 获取的数据中填充ListView。我正在使用扩展BaseAdapterCustomAdapter 来用数据填充ListViewListView 的每一行包含 2 个 TextView 和 1 个 CheckBox。现在我要做的是在Activity 的顶部放置一个EditText,当用户键入一些文本时,例如J ListView 应该显示所有以J 开头的联系人。请指导我。

MYCUSTOMADAPTER 代码

public class MyCustomAdapter extends BaseAdapter {

Context mContext;
private LayoutInflater mInflater;
SparseBooleanArray mSparseBooleanArray;
private ArrayList<String> mData = new ArrayList<String>();
private ArrayList<String> mNumber = new ArrayList<String>();

public MyCustomAdapter(Context mContext) {
    mInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mSparseBooleanArray = new SparseBooleanArray();
}

public ArrayList<String> getCheckedItems() {
    ArrayList<String> mTempArry = new ArrayList<String>();
    for (int i = 0; i < mData.size(); i++) {
        if (mSparseBooleanArray.get(i)) {
            //mTempArry.add(mData.get(i));
            mTempArry.add(mNumber.get(i));
        }
    }
    return mTempArry;
}

public int getCount() {
    return this.mData.size();
}

public void addItem(String paramString1, String paramString2) {
    this.mData.add(paramString1);
    this.mNumber.add(paramString2);
    notifyDataSetChanged();
}

public Object getItem(int paramInt) {
    return (String) this.mData.get(paramInt);
}

public long getItemId(int paramInt) {
    return paramInt;
}

public View getView(final int paramInt, View paramView,
        ViewGroup paramViewGroup) {
    if (paramView == null) {
        paramView = mInflater.inflate(R.layout.multiplecontactview, null);
    }
    TextView txtName = (TextView) paramView.findViewById(R.id.txtContactName);
    TextView txtNumber = (TextView) paramView.findViewById(R.id.txtContactNumber);
    CheckBox mCheckBox = (CheckBox) paramView.findViewById(R.id.checkBox1);
    mCheckBox.setTag(paramInt);
    mCheckBox.setChecked(mSparseBooleanArray.get(paramInt));
    mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
    txtName.setTextColor(Color.BLACK);
    txtNumber.setTextColor(Color.BLACK);
    for (int i = 0; i < mData.size(); i++) {
        txtName.setText(mData.get(paramInt).toString());
        txtNumber.setText(mNumber.get(paramInt).toString());
    }
    return paramView;
}

OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
    }
};

public static class ViewHolder {
    public CheckBox cb;
    public TextView nameView;
    public TextView numberView;
}

}

用数据 ONCREATE() 填充 LISTVIEW 的代码

ArrayList<Contact> contacts = db.getAllContacts();
        for (Contact cn : contacts) {
            mAdapter.addItem(cn.getName(), cn.getPhoneNumber());
        }
        mAdapter.notifyDataSetChanged();
        mListView.setAdapter(mAdapter);

【问题讨论】:

  • stackoverflow.com/questions/13090046/…。此链接可能会对您有所帮助。
  • 您建议的链接包含 customadapter 扩展 arrayadapter 而我扩展 baeseadapter 的示例。
  • 链接就像一个参考而不是实际的代码。您可以以自己的方式实施。至少你有一个开始的想法。对于答案,你可以看到下面的答案。我只是建议,所以我把它放在评论中。

标签: android baseadapter custom-adapter


【解决方案1】:

我必须为我不久前制作的应用程序实现此功能。由于它是开源的,您可以查看代码。我为此使用了SearchView

https://github.com/royale1223/HwaSettings

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2021-09-18
    相关资源
    最近更新 更多