Here I am using adapter to show multiple selection list view with search functionality but when i select the item without filtering list view getting me correct checked list items but when i filtered list view getting wrong checked item, then how to save state of checked item when filtered list.Following is my adapter class code.
public class List_Filter_Multi_SelectAdapter extends ArrayAdapter<List_Filter> {
private Context context;
private List<List_Filter> user_list;
private List<List_Filter>original_userList;
private Filter userFilter;
private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();
/* public List_Filter_Multi_SelectAdapter(Context context, int resource,
int textViewResourceId, List_Filter[] objects) {
super(context, resource, textViewResourceId, objects);
for(int i = 0; i < objects.length; i++){
myChecked.put(i, false);
}
}*/
public List_Filter_Multi_SelectAdapter(List<List_Filter> user_list, Context ctx) {
super(ctx, R.layout.row_for_multiple_selection, user_list);
this.user_list = user_list;
this.context = ctx;
this.original_userList = user_list;
for(int i = 0; i < user_list.size(); i++){
myChecked.put(i, false);
}
}
public int getCount() {
return user_list.size();
}
public List_Filter getItem(int position) {
return user_list.get(position);
}
public long getItemId(int position) {
return user_list.get(position).hashCode();
}
private static class memberHolder {
public TextView memberNameView;
}
/*public void filter(String charText) {
// TODO Auto-generated method stub
charText = charText.toLowerCase(Locale.getDefault());
filterOfMember.clear();
for (String element :listOfuserName) {
if (element.toLowerCase().contains(charText.toString().toLowerCase())) {
filterOfMember.add(element);
}
}
notifyDataSetChanged();
}*/
public void toggleChecked(int position){
if(myChecked.get(position)){
myChecked.put(position, false);
}else{
myChecked.put(position, true);
}
notifyDataSetChanged();
}
public List<Integer> getCheckedItemPositions(){
List<Integer> checkedItemPositions = new ArrayList<Integer>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItemPositions).add(i);
}
}
return checkedItemPositions;
}
public List<String> getCheckedItems(){
List<String> checkedItems = new ArrayList<String>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
List_Filter p = user_list.get(i);
(checkedItems).add(p.getName());
}
}
return checkedItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
memberHolder holder=new memberHolder();
CheckedTextView checkedTextView;
if(row==null){
List_Filter p = user_list.get(position);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=li.inflate(R.layout.row_for_multiple_selection, parent, false);
TextView tv = (TextView) row.findViewById(R.id.textForMultiselection);
holder.memberNameView = tv;
row.setTag(holder);
checkedTextView = (CheckedTextView)row.findViewById(R.id.textForMultiselection);
checkedTextView.setText((CharSequence) p.getName());
}else{
List_Filter p = user_list.get(position);
holder = (memberHolder) row.getTag();
//List_Filter p = user_list.get(position);
holder.memberNameView.setText(p.getName());
checkedTextView = (CheckedTextView)row.findViewById(R.id.textForMultiselection);
checkedTextView.setText(holder.memberNameView.getText());
}
Boolean checked = myChecked.get(position);
if (checked != null) {
checkedTextView.setChecked(checked);
}
return row;
}
public void resetData() {
user_list = original_userList;
}
@Override
public Filter getFilter() {
if (userFilter == null)
userFilter = new userNameFilter();
return userFilter;
}
private class userNameFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = original_userList;
results.count = original_userList.size();
}
else {
// We perform filtering operation
List<List_Filter> nUserList = new ArrayList<List_Filter>();
for (List_Filter p : user_list) {
if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
nUserList.add(p);
}
results.values = nUserList;
results.count = nUserList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// Now we have to inform the adapter about the new list filtered
if (results.count == 0)
notifyDataSetInvalidated();
else {
user_list = (List<List_Filter>) results.values;
notifyDataSetChanged();
}
}
}
}
Because in your myChecked list you save the real position of item (their position in original_userList not their position in the list after filter). you can add a boolean parameter to your List_Filter.class to save check state or you create another class that extands from List_Filter and add this parameter
and your code become: