在这里提问之前,我看过this thread。但是该线程包含简单字符串数组的示例,而在我的情况下,我使用的是ArrayList<Map<String, String>>
,所以我现在有点困惑。
我的android活动中有多个选择listview
。我使用listview
显示联系人列表,以便用户可以一次选择多个电话号码。但是现在我想为相同的目的添加搜索功能。我想要做的是在顶部放置一个EditText
,并在EditText
中输入任何字符用户,我希望ListView
根据用户输入填充过滤数据。我有一个CustomAdapter
扩展BaseAdapter
和一个名为Contact
的POJO类,我将其用于GET
&amp; SET
联系方式。
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);
}
};
}
联系课程
public class Contact {
int _id;
String _name;
String _phone_number;
public Contact() {
}
public Contact(int id, String name, String _phone_number) {
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
public Contact(int id) {
this._id = id;
}
public Contact(String name, String _phone_number) {
this._name = name;
this._phone_number = _phone_number;
}
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String getName() {
return this._name;
}
public void setName(String name) {
this._name = name;
}
public String getPhoneNumber() {
return this._phone_number;
}
public void setPhoneNumber(String phone_number) {
this._phone_number = phone_number;
}
}
答案 0 :(得分:0)
为了能够做到这一点,您需要实现Textwatcher
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(...);
}
}
};
然后将filterTextWatcher设置为您的editext:
searchEditText.addTextChangedListener(filterTextWatcher);
希望有所帮助! :)