1

I'm trying to add some checkboxes my recyclerview, anytime that I scroll the list, all checkboxes will be checked and unchecked! on creating the list, I need to make some checkboxes checked by default and I don't want to all checkboxes to be unchecked or checked when I scroll the list.

 @Override
    public Holder onCreateViewHolder(ViewGroup viewGroup) {
        View view = LayoutInflater.from(viewGroup.getContext())
                                  .inflate(R.layout.inf_add_manager, viewGroup, false);
        Holder holder = new Holder(view, true);

        holder.checkbox.setOnCheckedChangeListener(this);

        for (Profile profile : mProfiles) {
            if (mComplex.getManagersId().contains(profile.getId()) && !checkedContactsId.contains
                    (profile.getId())) {
                checkedContactsId.add(profile.getId());
            }
        }

        return holder;
    }

@Override
    public void onBindViewHolder(Holder holder, int position) {
        Profile item = mProfiles.get(position);

        holder.checkbox.setTag(item.getId());

        holder.checkbox.setChecked(checkedContactsId.contains(item.getId()));
    }

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            checkedContactsId.add((String) buttonView.getTag());
        } else {
            checkedContactsId.remove(buttonView.getTag());
        }
    }
Alex
  • 1,623
  • 1
  • 24
  • 48
  • possible duplicate of : (different symptom with same cause): http://stackoverflow.com/questions/32497092/how-to-increment-a-value-on-view-holder-variables-in-android – k3b Sep 10 '15 at 09:29

1 Answers1

0

A way to do it to have a flag in your Profile POJO and depending on the value of the flag in your current object you can set if you want your check box to be checked or unchecked. Hope this helps :)

PunK _l_ RuLz
  • 621
  • 6
  • 20