Android 如何在listview上实现过滤器
在这里提问之前,我看了一下。但该线程包含简单字符串数组的示例,而在我的例子中,我使用的是Android 如何在listview上实现过滤器,android,filter,android-listview,Android,Filter,Android Listview,在这里提问之前,我看了一下。但该线程包含简单字符串数组的示例,而在我的例子中,我使用的是ArrayList,所以我现在有点困惑 在我的android活动中,我有一个多选项listview。我正在使用列表视图显示联系人列表,以便用户一次可以选择多个电话号码。但现在我想添加搜索功能,以达到同样的目的。我想做的是在顶部放置一个EditText,无论用户在该EditText中键入什么字符,我都希望ListView中根据用户输入填充过滤数据。我有一个CustomAdapter,它扩展了BaseAdapte
ArrayList
,所以我现在有点困惑
在我的android活动中,我有一个多选项listview
。我正在使用列表视图
显示联系人列表,以便用户一次可以选择多个电话号码。但现在我想添加搜索功能,以达到同样的目的。我想做的是在顶部放置一个EditText
,无论用户在该EditText
中键入什么字符,我都希望ListView
中根据用户输入填充过滤数据。我有一个CustomAdapter
,它扩展了BaseAdapter
,还有一个名为Contact
的POJO类,我正在使用它来获取
和设置
联系人详细信息
MYCUSTOMADAPTER.JAVA
public class MyCustomAdapter extends BaseAdapter {
Context mContext;
private LayoutInflater mInflater;
SparseBooleanArray mSparseBooleanArray;
private ArrayList<Map<String,String>> mAdapData = new ArrayList<Map<String, 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 < mAdapData.size(); i++) {
if (mSparseBooleanArray.get(i)) {
Map<String, String> map = (Map<String, String>) mAdapData.get(i);
final String numbr = map.get("Phone").toString();
mTempArry.add(numbr);
}
}
return mTempArry;
}
public int getCount() {
return this.mAdapData.size();
}
public void addItem(String paramString1, String paramString2) {
Map<String, String> NameNumber = new HashMap<String, String>();
NameNumber.put("Name", paramString1);
NameNumber.put("Phone", paramString2);
this.mAdapData.add(NameNumber);
notifyDataSetChanged();
}
@SuppressWarnings("unchecked")
public Object getItem(int paramInt) {
return (ArrayList<Map<String, String>>) this.mAdapData.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 < mAdapData.size(); i++) {
Map<String, String> map = (Map<String, String>) mAdapData.get(paramInt);
final String name = map.get("Name").toString();
final String numbr = map.get("Phone").toString();
txtName.setText(name);
txtNumber.setText(numbr);
}
return paramView;
}
OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
};
}
要做到这一点,您需要实现 然后将filterTextWatcher设置为您的编辑文本:
searchEditText.addTextChangedListener(filterTextWatcher);
希望有帮助!:)
private TextWatcher filterTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void afterTextChanged(Editable arg0) {
/*clear previous contents of list*/
adapter.clear();
/*add new contents according to the filter*/
String filter=arg0.getText().toString();
if (filter == null
|| Pattern
.compile(Pattern.quote(filter),
Pattern.CASE_INSENSITIVE)
.matcher(adapter.getItem(...)).find()
|| filter.equals("")) {
adapter.addItem(...);
}
}
};
searchEditText.addTextChangedListener(filterTextWatcher);