在从数据库填充的listview上实现搜索过滤器

时间:2012-11-28 15:36:38

标签: android baseadapter custom-adapter

我从ListView提取的数据填充Database。我正在使用CustomAdapter扩展BaseAdapter以填充ListView数据。 ListView的每一行包含2 TextView和1 CheckBox。现在我要做的是在EditText的顶部放置一个Activity,当用户为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;
}

}

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

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

1 个答案:

答案 0 :(得分:0)

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

https://github.com/royale1223/HwaSettings