Question

I have some problem deselecting checkbox after I check them all.
here you are a brief overview of my code.
I populate a list item with a custom adapter, called "MyAdapter", in this adapter I use:
1) a boolean array to save witch checkbox is checket and witch not
2)An ArrayList of "Users" where User is a class containing many info, like telephone number and name of the contacts
3)An ArrayList where I add only users checked.

The problem is: if I manually check all checkbox and then uncheck some, all works.Vut if I use the checkAll() method I will show you soon, and then manually uncheck some, it works only graphically ( checkbox are unchecked ), but user unchecked isn't removed from the correspondent array list.

Here is my code, any hind will be appreciated:

public class MyAdapter extends ArrayAdapter<User> {
private boolean[] checkList;
private ArrayList<User> checkedUsersList;
private LinkedList<User> originalList;
private int listSize;
private Context mContext;

public MyAdapter(Context context,int textViewResourceId,
        List<User> objects) {
    super(context,textViewResourceId, objects);
    mContext = context;
    checkedUsersList = new ArrayList<User>();
    originalList = (LinkedList<User>) objects;
    checkList = new boolean[objects.size()];
    listSize = objects.size();
    for(int i=0;i<objects.size();i++)
    {
        checkList[i] = false;
    }
    // TODO Auto-generated constructor stub
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getViewOptimize(position, convertView, parent);
}

 public View getViewOptimize(int position, View convertView, ViewGroup parent) {
     final int pos = position;
        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.rowcustom, null);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView)convertView.findViewById(R.id.textViewName);
            viewHolder.number = (TextView)convertView.findViewById(R.id.textViewNumber);
            viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBoxUserLine);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final User utente = getItem(position);

        viewHolder.name.setText(utente.getContactName());
        viewHolder.number.setText(utente.getContactNumber());
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked)
                {
                    Toast toast = Toast.makeText(mContext, "Check true notified", 1500);
                    toast.show();
                    if(!checkedUsersList.contains(utente))
                        checkedUsersList.add(utente);

                }
                else 
                {
                    Toast toast = Toast.makeText(mContext, "Check false notified", 1500);
                    toast.show();
                    checkedUsersList.remove(utente);

                }
                checkList[pos] = isChecked;
            }
        });
        viewHolder.checkBox.setChecked(checkList[pos]);
        return convertView;
    }


 public ArrayList<User> getUsersChecked()
 {
     return checkedUsersList;
 }

 public void checkAll()
 {

     for(int i = 0; i<listSize;i++)
     {
         checkList[i] = true;

     }

     checkedUsersList.clear();
     for(User us : originalList)
     {
         checkedUsersList.add(new User(us.getContactName(), us.getContactNumber()));
     }

     notifyDataSetChanged();

 }


    private class ViewHolder {
        public TextView name;
        public TextView number;
        public CheckBox checkBox;
    }



}
Was it helpful?

Solution

for(User us : originalList)
 {
     checkedUsersList.add(new User(us.getContactName(), us.getContactNumber()));
 }

Add us to the list, not a new user.

OTHER TIPS

When adding all the users to the list in checkAll you are creating new users:

     for(User us : originalList)
     {
         checkedUsersList.add(new User(us.getContactName(), us.getContactNumber()));
     }

replace with

     for(User us : originalList)
     {
         checkedUsersList.add(us);
     }

This is the solution. The problem is that you are comparing the original object to a copy of the object in your checkedUsersList.contains(utente)

Maintain the state of the checkbox for the list by making an array of boolean.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top