I had a look at this thread before asking a question here. But that thread contains example of simple string array whereas in my case I am using ArrayList<Map<String, String>> so I am bit confused at the moment.
I have a multiple selection listview in my android activity. I am using the listview to display the list of contacts so that the user can select multiple phone numbers at a time. But now I want to add the search functionality for the same purpose. What I want to do is place one EditText at the top and whatever character users types in that EditText, I want the ListView to be filled with filtered data based on users input. I have a CustomAdapter which extends BaseAdapter and a POJO class named Contact which I am using to GET & SET contact details.
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 CLASS
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;
}
}
For you to be able to do that, you need to implement a Textwatcher
Then set that filterTextWatcher to your editext:
Hope that helps! 🙂