Android RecyclerView-类不是抽象的,并且没有实现抽象基类成员

时间:2019-02-20 19:52:13

标签: android kotlin android-recyclerview

我明白了:

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

titleremove上未解析的引用

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

onBindViewHolder()的签名更改为此:

override fun onBindViewHolder(holder: ViewHolder, position: Int)

第一个参数的类型为ViewHolder而不是RecyclerView.ViewHolder

答案 1 :(得分:0)

第一个问题:

您没有覆盖正确的方法。在构造函数中,您声明要使用的ViewHolderRecentAdapter.ViewHolder,但是您的onBindViewHolder引用了通用RecyclerView.ViewHolder。要解决此问题,只需更改

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

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

对于第二个问题,我不确定给定文件有什么错误。确保您要在videoViewTitle布局文件中找到带有ID videoViewRemovevideoview的视图吗?