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

时间:2018-07-15 03:21:30

标签: android android-studio kotlin

请帮助我,我无法在Kotlin的RecyclerView中实现onBindViewHolder。我想使用Kotlin语言android studio中的Reyclerview为我的数据卡制作适配器。但是当我实现onBindViewHolder时,我的代码将引发错误。请帮忙。

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

1 个答案:

答案 0 :(得分:4)

只需更改此行

class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<RecyclerView.ViewHolder>() {

class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<MissionViewAdapter.CardViewHolder>() {

实际上,您正在使用RecyclerView的主要ViewHolder类。这就是为什么您的参数错误的bindViewHolder方法,并且您会收到该错误。在这种情况下,您的onBindViewHolder方法应如下所示:

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