我明白了:
class RecentAdapter(private val context: Context, private val videolist: MutableList<Videos>) : RecyclerView.Adapter<RecentAdapter.ViewHolder>(){
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val video = videolist[position]
holder.title.text = video.title
holder.remove.setOnClickListener {
videolist.removeAt(holder.adapterPosition)
notifyItemRemoved(holder.adapterPosition)
}
}
override fun getItemCount() = videolist.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.videoview, parent, false)
return ViewHolder(view)
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!){
val title = itemView!!.videoviewTitle!!
val remove = itemView!!.videoviewRemove!!
val like = itemView!!.videoviewLike!!
}
}
我得到了错误:
Class 'RecentAdapter' is not abstract and does not implement abstract base class member public abstract fun onBindViewHolder(@NonNull p0: RecentAdapter.ViewHolder, p1: Int): Unit defined in android.support.v7.widget.RecyclerView.Adapter
在class RecentAdapter
和
'onBindViewHolder' overrides nothing
和
title
和remove
上未解析的引用
有人可以帮我吗?
答案 0 :(得分:1)
将onBindViewHolder()
的签名更改为此:
override fun onBindViewHolder(holder: ViewHolder, position: Int)
第一个参数的类型为ViewHolder
而不是RecyclerView.ViewHolder
答案 1 :(得分:0)
第一个问题:
您没有覆盖正确的方法。在构造函数中,您声明要使用的ViewHolder
是RecentAdapter.ViewHolder
,但是您的onBindViewHolder
引用了通用RecyclerView.ViewHolder
。要解决此问题,只需更改
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
到
override fun onBindViewHolder(holder: RecentAdapter.ViewHolder, position: Int) {
对于第二个问题,我不确定给定文件有什么错误。确保您要在videoViewTitle
布局文件中找到带有ID videoViewRemove
和videoview
的视图吗?