Hello, I just implemented checkbox on my recycler ...
# getting-started
d
Hello, I just implemented checkbox on my recycler view, but I think I encountered a bug, so whenever I clicked on the first index, suddenly the item on the 11th index get clicked, it also apply whenever I click on 2nd item, the 12th item also get clicked. Is this a bug ? I need some assistance, thanks. For additional info, my adapter :
Copy code
class RecyclerViewAdapter(val feed: List<ApprovalpenyimpanganActivity.Penyimpangan>): RecyclerView.Adapter<CustomViewHolder>(){


    override fun getItemCount(): Int {
        return feed.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutinflater = LayoutInflater.from(parent?.context)
        val row = layoutinflater.inflate(R.layout.penyimpanganlayout, parent, false)
        return CustomViewHolder(row)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int){
        val feed = feed.get(position)
        holder.view.textView5.text = feed.namakaryawan + " - " + feed.kodenik + "\n" +
                                        feed.jamin + " - " + feed.jamout + "\n" +
                                        "Tanggal : " + feed.tglabsen
        holder.view.checkBox.setOnClickListener{
            println(position)
            println("CLICKED")
            println(feed)
        }
    }

}

class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){

}
My data class :
Copy code
data class Penyimpangan(
        @SerializedName("kodenik") val kodenik : String,
        @SerializedName("namakaryawan") val namakaryawan : String,
        @SerializedName("kodebagian") val kodebagian : String,
        @SerializedName("tglupdate") val tglupdate : String,
        @SerializedName("tglabsen") val tglabsen : String,
        @SerializedName("jamin") val jamin : String,
        @SerializedName("jamout") val jamout : String
    )
b
The way a RecyclerView works is by reusing the same views in the list. So when you scroll to the 11th item it’s reusing the view from the 1st item, which has its checkbox in the checked state. You need to track the state of the checkbox yourself in your data class and then set it in
onBindViewHolder()
Also this isn’t a Kotlin issue, so in future post this kinda thing to Stackoverflow 🙂
r
Btw there is an #android channel if you’ll have a Kotlin problem related to android
d
Sorry, my bad didn't see it