gpt4 book ai didi

android - 在 Kotlin 中使用泛型时发现类型不匹配

转载 作者:行者123 更新时间:2023-11-29 02:24:48 24 4
gpt4 key购买 nike

我正在转换 this code到 Kotlin

在哪里

abstract class BaseModel(){
}

BaseViewHolder

abstract class BaseViewHolder<T : BaseModel>(itemView: View) : RecyclerView.ViewHolder(itemView){
abstract fun bindData(data: T)
}

基础适配器

abstract class BaseAdapter<T:BaseModel, U : BaseViewHolder<*>>(var items: List<T>) : RecyclerView.Adapter<U>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): U {
}

override fun getItemCount(): Int {
return items.size
}

override fun onBindViewHolder(holder: U, pos: Int) {
holder.bindData(items.get(pos))
}
}

onBindViewHolder 方法 holder.bindData 中给出错误 type mismatch require Nothing found T

我做错了什么??

最佳答案

更改您的 BaseViewHolder<*>来自 *T

就这样

...BaseAdapter<T:BaseModel, U : BaseViewHolder<T>>...

关于android - 在 Kotlin 中使用泛型时发现类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52886383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com