Please help me, I can't implement onBindViewHolder
in my RecyclerView in Kotlin. I want to make an adapter for my data cards using Reyclerview in Kotlin language android studio. But when I implement onBindViewHolder
my code throws an error. Please help.
package com.brid.azis.vipgame.test.Adapter
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import android.widget.TextView
import com.brid.azis.vipgame.R
import com.brid.azis.vipgame.test.DataModel.DataCard
class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
CardViewHolder(LayoutInflater.from(context).inflate(R.layout.item_mission,parent,false))
override fun getItemCount(): Int = cards.size
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
holder.bindCards(cards[position])
}
class CardViewHolder(view:View):RecyclerView.ViewHolder(view) {
val judul = view.findViewById<TextView>(R.id.tv_judulkartu)
val petunjuk = view.findViewById<TextView>(R.id.tv_petunjukkartu)
val tanggal = view.findViewById<TextView>(R.id.tv_tanggalmasukkartu)
fun bindCards(cards:DataCard) {
judul.text = cards.judul
petunjuk.text = cards.petunjuk
tanggal.text = cards.tanggal
}
}
}
This is the error:
Just change this line
class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
to
class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<MissionViewAdapter.CardViewHolder>() {
Actually, you are using the main ViewHolder
class of RecyclerView. That's why your parameters are wrong of bindViewHolder
method and you are getting that error. In this case, your onBindViewHolder
method should look like this:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With