mchd
mchd

Reputation: 3163

RecyclerView doesn't recognize the view holder

I'm learning Kotlin by building an app and now, I'm trying to create an adapter for my RecyclerView. However, the name that I chose for my ViewGroup is marked as Unresolved Reference. I'm fairly new to Kotlin so I knew I was going to make a dumb mistake but in this case, I couldn't figure out what I did wrong.

class RecyclerAdapter: RecyclerView.Adapter<RecyclerAdapter.PosterHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getItemCount(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onBindViewHolder(holder: RecyclerAdapter.PosterHolder, position: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

}

class PosterHolder (v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
    override fun onClick(p0: View?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

Upvotes: 2

Views: 1258

Answers (2)

Tung Tran
Tung Tran

Reputation: 2955

Try this to fix:

class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.PosterHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PosterHolder? {
        return null
    }

    override fun onBindViewHolder(holder: PosterHolder, position: Int) {

    }

    override fun getItemCount(): Int {
        return 0
    }

    inner class PosterHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}

And to convert java file to kotlin file with Android Studio, choosing Code->Convert java file to kotlin file.

Upvotes: 1

mchd
mchd

Reputation: 3163

I literally had to re-implement the functions. I think this is an Android Studio issue, as opposed to Kotlin. Here is my most up to date code:

class RecyclerAdapter: RecyclerView.Adapter<PosterHolder>(){

    override fun onBindViewHolder(holder: PosterHolder, position: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PosterHolder{
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getItemCount(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

}

class PosterHolder (v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
    override fun onClick(p0: View?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

Upvotes: 0

Related Questions