简体   繁体   English

为什么'onBindViewHolder'覆盖什么Kotlin?

[英]why 'onBindViewHolder' Overrides Nothing Kotlin?

Please help me, I can't implement onBindViewHolder in my RecyclerView in Kotlin. 请帮帮我,我无法在Kotlin的RecyclerView中实现onBindViewHolder I want to make an adapter for my data cards using Reyclerview in Kotlin language android studio. 我想在Kotlin语言android studio中使用Reyclerview为我的数据卡制作适配器。 But when I implement onBindViewHolder my code throws an error. 但是当我实现onBindViewHolder我的代码会抛出错误。 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. 实际上,您正在使用RecyclerView的主ViewHolder类。 That's why your parameters are wrong of bindViewHolder method and you are getting that error. 这就是为什么你的参数错误的bindViewHolder方法,你得到的错误。 In this case, your onBindViewHolder method should look like this: 在这种情况下,您的onBindViewHolder方法应如下所示:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM