Android 在从数据库填充的listview上实现搜索筛选器
我正在从Android 在从数据库填充的listview上实现搜索筛选器,android,baseadapter,custom-adapter,Android,Baseadapter,Custom Adapter,我正在从数据库获取的数据中填写列表视图。我正在使用一个扩展了BaseAdapter的CustomAdapter来用数据填充ListView。列表视图的每一行包含2个文本视图和1个复选框。现在,我想做的是在活动的顶部放置一个EditText,当用户为例如J键入一些文本时,列表视图应显示以J开头的所有联系人。请指导我 MYCUSTOMADAPTER的代码 public class MyCustomAdapter extends BaseAdapter { Context mContext; pri
数据库
获取的数据中填写列表视图
。我正在使用一个扩展了BaseAdapter
的CustomAdapter
来用数据填充ListView
。列表视图的每一行包含2个文本视图
和1个复选框
。现在,我想做的是在活动的顶部放置一个EditText
,当用户为例如J
键入一些文本时,列表视图应显示以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;
}
}
我必须为我不久前制作的一个应用程序实现这一点。因为它是开源的,你可以看看代码。我为此使用了SearchView
。此链接可能会对您有所帮助。您建议的链接包括customadapter扩展arrayadapter而我扩展baeseadapter的示例。此链接类似于参考,而不是实际代码。你必须以你自己的方式来实施。至少你有一个开始的想法。有关答案,请参见下面的答案。我只是建议,所以我把它放在评论里。
ArrayList<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
mAdapter.addItem(cn.getName(), cn.getPhoneNumber());
}
mAdapter.notifyDataSetChanged();
mListView.setAdapter(mAdapter);