1
我在看這個問題之前先看看this thread。但是該線程包含簡單字符串數組的示例,而在我的情況下,我使用的是ArrayList<Map<String, String>>
,所以我現在有點困惑。如何在列表視圖上實現過濾器
我在我的android活動中有多個選擇listview
。我正在使用listview
來顯示聯繫人列表,以便用戶可以一次選擇多個電話號碼。但是現在我想爲相同的目的添加搜索功能。我想要做的是在頂部放置一個EditText
,並且無論用戶輸入哪個字符EditText
,我都希望ListView
可以根據用戶輸入填充過濾的數據。我有一個CustomAdapter
其中BaseAdapter
和POJO類名爲Contact
我正在使用GET
& 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);
}
};
}
Contact類
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;
}
}