title: How to iterate through a Userlist in android RecyclerView and save CheckBox changes on a Firestore Database with Kotlin
body: <p>I have been trying for few days to find a solution , but I can not find the hot potato.</p>
  
  <p>I have about 200 users in attendance list for a sports club.
  The checkbox should store which users are present.
  The problem is that I can only manage only one user.
  I try a lot but no result
  Anybody have an idea how to solve this problem please?</p>
  
  <p><strong>UPDATE:</strong>
  Thanks to </p>
  
  <blockquote>
    <p>@Sanlok Lee</p>
  </blockquote>
  
  <p>Problem successfully resolved.</p>
  
  <p>Below the new code:</p>
  
  <p>class User:</p>
  
  <pre><code>    class User(
      val uid: String?,
      val username: String?,
      val profileImageUrl: String?,
      val isSelected: Boolean?
  )  {
  
      constructor() : this("", "", "", false)
  }
  </code></pre>
  
  <p>class Home:</p>
  
  <pre><code>class Home : AppCompatActivity() {
  val db = FirebaseFirestore.getInstance()
  val adapter = PerformListAdapter()
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_perform_anwesenheitsliste)
  
      verifyUserIsLogin()
  
      _recyclerView_show_anwesenheitsliste.adapter = adapter
  
      showUsersAndPerformChekBoxes()
  }
  
  fun showUsersAndPerformChekBoxes() {
      db.collection("users")
          .get()
          .addOnSuccessListener { documents -&gt;
              val users = documents.map { doc -&gt;
                  doc.toObject(User::class.java)
              }
              adapter.submitUsers(users)
          }
  }
  
  private fun verifyUserIsLogin() {
      val uid = FirebaseAuth.getInstance().uid
      if (uid == null) {
          val intent = Intent(this, LoginActivity::class.java)
          intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
          startActivity(intent)
      }
  }
  
  override fun onOptionsItemSelected(item: MenuItem?): Boolean {
      when (item?.itemId) {
  
          R.id.menu_home -&gt; {}
          R.id.menu_sign_out -&gt; {
              FirebaseAuth.getInstance().signOut()
              val intent = Intent(this, RegisterActivity::class.java)
              intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
              startActivity(intent)
          }
      }
  
      return super.onOptionsItemSelected(item)
  }
  
  override fun onCreateOptionsMenu(menu: Menu?): Boolean {
      menuInflater.inflate(R.menu.nav_menu_home, menu)
      return super.onCreateOptionsMenu(menu)
  }
  }
  </code></pre>
  
  <p>class PerformListAdapter:</p>
  
  <pre><code>class PerformListAdapter : RecyclerView.Adapter&lt;PerformListAdapter.CustomViewHolder&gt;() {
      val db = FirebaseFirestore.getInstance()
  
      var users: List&lt;User&gt; = ArrayList()
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
  
          val inflater = LayoutInflater.from(parent.context)
          val itemuViewRow = inflater.inflate(R.layout.user_row, parent, false)
          return CustomViewHolder(itemuViewRow)
      }
  
      override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
          if (users.size &gt; position) {
              holder.bindTo(users[position])
          } else {
              holder.clear()
          }
      }
  
      override fun getItemCount(): Int {
          return users.size
      }
  
      fun submitUsers(users: List&lt;User&gt;) {
          this.users = users
          notifyDataSetChanged()
      }
  
      inner class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view) {
          private val checkBox_select: CheckBox = view.findViewById(R.id._checkBox_select)
          private val textView_anwesenheitsliste_username =
              view.findViewById&lt;TextView&gt;(R.id._textView_anwesenheitsliste_username)
  
          private val imageView_show_anwesenheitsliste_row =
              view.findViewById&lt;CircleImageView&gt;(R.id._imageView_show_anwesenheitsliste_row)
  
          private val checkedChangeListener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked -&gt;
  
              val uid = users[adapterPosition].uid
  
              if (uid != null) {
                  Log.d(TAG, uid)
                  if (isChecked) {
                      db.collection("users")
                          .document(uid)
                          .update("selected", true)
                  } else {
                      db.collection("users")
                          .document(uid)
                          .update("selected", false)
                  }
              }
          }
  
          fun bindTo(user: User) {
              if (!user.profileImageUrl!!.isEmpty()) {
                  Picasso.get().load(user.profileImageUrl)
                      .into(imageView_show_anwesenheitsliste_row)
              }
              textView_anwesenheitsliste_username?.text = user.username
              checkBox_select.setOnCheckedChangeListener(null)
              checkBox_select.isChecked = (user.isSelected == true)
              checkBox_select.setOnCheckedChangeListener(checkedChangeListener)
          }
  
          fun clear() {
              textView_anwesenheitsliste_username.text = ""
              checkBox_select.setOnCheckedChangeListener(null)
          }
      }
  }
  </code></pre>