[英]Kotlin: Type mismatch (RecyclerView.Adapter<RecyclerView.ViewHolder>)
I'm pretty new using Kotlin, I tried to port my project from java to kotlin.我是使用 Kotlin 的新手,我尝试将我的项目从 java 移植到 kotlin。 Basically, I have a helper class that try to init recyclerview layout, adapter and so on.基本上,我有一个帮助类尝试初始化 recyclerview 布局、适配器等。
In java: Adapter在 java 中:适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
...
public static class ViewHolder extends RecyclerView.ViewHolder
{
...
}
...
}
my helper function:我的辅助功能:
public void init(RecyclerView recyclerView, boolean horizontal, boolean divider, RecyclerView.Adapter adapter, Context context)
When I created the adapter and call the function, it works fine.当我创建适配器并调用该函数时,它工作正常。
But in Kotlin: Adapter但在 Kotlin 中:适配器
class MyAdapter(...) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
...
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
...
}
...
}
helper function辅助函数
fun init(recyclerView : RecyclerView, horizontal: Boolean, divider: Boolean, adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>, context: Context) {
...
}
when I created the adapter and call the function当我创建适配器并调用函数时
val myAdapter = MyAdapter(...)
helper.init(listView, false, false, myAdapter, this)
I got mismatch error where it required: RecyclerView.Adapter<RecyclerView.ViewHolder>
and found: MyAdapter
我在需要的地方遇到不匹配错误: RecyclerView.Adapter<RecyclerView.ViewHolder>
并找到: MyAdapter
Inheritance wise, it should work right?继承明智,它应该工作对吗? or did I miss something?还是我错过了什么?
Thanks!谢谢!
Not quite, the correct declaration you require is:不完全是,您需要的正确声明是:
class MyAdapter() : RecyclerView.Adapter<RecyclerView.ViewHolder>()
and you then need to implement methods such as this signature:然后您需要实现诸如此签名之类的方法:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder
etc.等等
onCreateViewHolder
must return a ViewHolder or a subclass of it, eg onCreateViewHolder
必须返回 ViewHolder 或其子类,例如
return ItemViewHolder(view)
where:-在哪里:-
inner class ItemViewHolder(val mView: View) : RecyclerView.ViewHolder(mView)
{
val mFieldView: TextView = mView.field
...
}
and you can use this knowledge within the bind function, eg您可以在绑定函数中使用这些知识,例如
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int)
{
val itemHolder = holder as ItemViewHolder
val item = dataModel[position]
itemHolder.mFieldView.text = item.field
}
Check out the type of adapter you are using, there are different types of adapter like array adapter, recyclerView Adapter.查看您正在使用的适配器类型,有不同类型的适配器,如数组适配器、recyclerView 适配器。 Specifically, in list views, we use an array adapter.具体来说,在列表视图中,我们使用数组适配器。 While with recycler View adapter used is recycler view adapter and it will show the above error if you use array adapter with recycler view.而使用回收器视图适配器是回收器视图适配器,如果您使用带有回收器视图的数组适配器,它将显示上述错误。
I had the same issue.我遇到过同样的问题。 Replace RecyclerView.Adapter<RecyclerView.ViewHolder>
with RecyclerView.Adapter<*>
将RecyclerView.Adapter<RecyclerView.ViewHolder>
替换为RecyclerView.Adapter<*>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.