Search code examples
androidkotlinandroid-recyclerviewandroid-viewbinding

chekbox always checking last item in recyclerview


i want to get item from checked checkbox in my recyclerview item, this my adapter

class SelectedListDateAdapter(var listDate: List<DateDay>, private val onItemCheckListener: OnItemCheckListener) :
RecyclerView.Adapter<SelectedListDateAdapter.SelectedListDateViewHolder>() {
lateinit var  binding: ItemCheckBoxDateBinding
inner class SelectedListDateViewHolder(item: ItemCheckBoxDateBinding) : RecyclerView.ViewHolder(item.root) {
    val checkBoxList = item.checkBox
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelectedListDateViewHolder {
    binding = ItemCheckBoxDateBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false
    )
    return SelectedListDateViewHolder(binding)
}

override fun onBindViewHolder(holder: SelectedListDateViewHolder, position: Int) {
    holder.checkBoxList.setOnCheckedChangeListener(null)
    holder.checkBoxList.isChecked = listDate[position].isSelected
    holder.itemView.apply {
        val currentItem = listDate[position]
        binding.tvDateList.text = listDate[position].date

        setOnClickListener {
            binding.checkBox.isChecked = !binding.checkBox.isChecked
            if (binding.checkBox.isChecked) {
                binding.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                    currentItem.isSelected = isChecked
                }
                onItemCheckListener.onItemCheck(currentItem)
            } else {
                binding.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                    currentItem.isSelected = isChecked
                }
                onItemCheckListener.onItemUncheck(currentItem)
            }
        }
    }
}

override fun getItemCount(): Int {
    return listDate.size
}

}

im referring to this question get list of checked item to make that adapter

yes, it get the item and remove them but everytime i click an item in recyclerview it always check and uncheck the last item

i have checking this question CheckBox in RecyclerView keeps on checking different items but my result still the same, any help is appreciated


Solution

  • Maybe viewHolder reuse the previous item.Try to update listData, not currentItem. And move the nested Listener

    override fun onBindViewHolder(holder: SelectedListDateViewHolder, position: Int) {
    
        holder.itemView.tvDateList.text = listDate[position].date
        holder.checkBoxList.isChecked = listDate[position].isChecked
        holder.checkBoxList.setOnClickListener {
    
           listDate[position].isSelected = holder.checkBoxList.isChecked
        }
    
        holder.itemView.setOnClickListener {
    
            holder.checkBoxList.isChecked = !holder.checkBoxList.isChecked
            listDate[position].isSelected = holder.checkBoxList.isChecked
    
            val currentItem = listDate[position]
            if (holder.checkBoxList.isChecked) {
    
                onItemCheckListener.onItemCheck(currentItem)
    
            } else {
    
                onItemCheckListener.onItemUncheck(currentItem)
            }
        }
    }