title: On Scrolling Check Boxes are getting Unchecked or Vice versa in a RecyclerView
body: <p>I have a check box in RecyclerView Adapter's View Item.<br>
  When scrolling, some check boxes are getting checked and some are getting unchecked.<br>
  I have taken help from <a href="https://stackoverflow.com/questions/42159900/checkboxes-gets-unchecked-in-recyclerview-upon-scrolling">this</a> and <a href="https://mobikul.com/get-selected-checkboxes-recyclerview/" rel="nofollow noreferrer">this</a><br>
  But I am unable to find the exact solution to this.</p>
  
  <p>Here is my code </p>
  
  <pre><code>public class AllContactsAdapter extends RecyclerView.Adapter&lt;AllContactsAdapter.ViewHolder&gt; {
      private Context context;
      private ArrayList&lt;PhoneContactsModel&gt; listOfContacts = new ArrayList&lt;&gt;();
      PhoneContactsModel phoneContactsModel;
  
      private ArrayList&lt;PhoneContactsModel&gt; copyOfListOfContacts = new ArrayList&lt;&gt;();
  
      public AllContactsAdapter(Context context, ArrayList&lt;PhoneContactsModel&gt; listOfContacts) {
          this.context = context;
          this.listOfContacts = listOfContacts;
          // copyOfListOfContacts.addAll(listOfContacts);
      }
  
      @Override
      public AllContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.
                  adapter_load_allusers_contacts_listitem, parent, false);
          ViewHolder viewHolder = new ViewHolder(view);
          return viewHolder;
      }
  
      @Override
      public void onBindViewHolder(final AllContactsAdapter.ViewHolder holder, final int position) {
          Log.d("tag", "adapter========3");
          phoneContactsModel = listOfContacts.get(position);
          holder.name.setText(phoneContactsModel.getContactName());
          holder.phoneNumber.setText(phoneContactsModel.getContactNumber());
          holder.checkBox.setSelected(phoneContactsModel.isChecked());
  
          holder.contactLayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  if (holder.checkBox.isChecked()) {
                      phoneContactsModel.setChecked(false);
                      holder.checkBox.setChecked(false);
                      copyOfListOfContacts.remove(listOfContacts.get(position));
  
                  } else {
                      phoneContactsModel.setChecked(true);
                      holder.checkBox.setChecked(true);
                      copyOfListOfContacts.add(listOfContacts.get(position));
                  }
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return listOfContacts.size();
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder  {
  
          private TextView name, phoneNumber;
          private CheckBox checkBox;
          private LinearLayout contactLayout;
  
          public ViewHolder(View view) {
              super(view);
              name = view.findViewById(R.id.contactNameId);
              phoneNumber = view.findViewById(R.id.contactNumberId);
              checkBox = view.findViewById(R.id.checkBoxId);
              contactLayout = view.findViewById(R.id.contactLayoutId);
          }
      }
  }
  </code></pre>
  
  <p>For example, I have checked 1,2,3 items in list. When I scrolled down and come up , then 13 is checked and 2 is unchecked along with other some items.Please help me. </p>
  
  <p>Any solution is appreciated. </p>