Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why 'onBindViewHolder' Overrides Nothing Kotlin?

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:

This is the error

like image 693
woyjez Avatar asked Dec 18 '22 22:12

woyjez


1 Answers

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) {
like image 98
Avijit Karmakar Avatar answered Jan 09 '23 00:01

Avijit Karmakar