0

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

Pif
  • 530
  • 2
  • 10
  • 20

1 Answers1

1

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)
        }
    }
}
Pif
  • 530
  • 2
  • 10
  • 20
GHH
  • 1,713
  • 1
  • 8
  • 21
  • im still getting the same result, nothing changes – Pif Dec 14 '20 at 01:56
  • @Pif I edit the answer. use `setOnClickListener` instead of `setOnCheckedChangeListener` please try it. – GHH Dec 14 '20 at 02:28
  • i've try it, nothing changes do you need additional information on my project ? – Pif Dec 14 '20 at 02:38
  • @Pif Can you share the activity and SelectedListDateViewHolder class? – GHH Dec 14 '20 at 02:50
  • i forgot to mention, im using this on fragment and that adapter is for my bottomsheet dialog. Here is my fragment https://pastebin.com/DNJtmK5X . The viewholder already included on my question `inner class SelectedListDateViewHolder(item: ItemCheckBoxDateBinding) : RecyclerView.ViewHolder(item.root) { val checkBoxList = item.checkBox } ` – Pif Dec 14 '20 at 03:02
  • sorry if my code is confusing, this is my model to hold data for recyclerview in arraylist https://pastebin.com/8nNC6yMk – Pif Dec 14 '20 at 05:23
  • can i ask one question ? im using this adapter for multiple recyclerview when i check other bottomSheet dialog in my project, the checkbox state still the same – Pif Dec 14 '20 at 06:15
  • @Pif This question is a bit complicated, maybe you can post a new question. – GHH Dec 14 '20 at 06:22
  • please check my question https://stackoverflow.com/questions/65298668/using-same-adapter-for-multiple-similar-reyclerview-implementation – Pif Dec 15 '20 at 01:08