Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin android studio holder.itemview issue

im trying to make this mobile note taking app based on a tut i followed on youtube his does not have this error ive watched over and over i have a file called NotesAdapter

     holder.itemView.titleTV.text = notesList[position]!!.title
        holder.itemView.descTV.text = notesList[position]!!.description
        holder.itemView.idTV.text = notesList[position]!!.id.toString()

the titleTV, descTV, & idTV give an error "unresolved reference: title TV" etc. this stops the app from running can anyone assist ?

package com.example.moradinotepad

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import io.realm.RealmResults


class NotesAdapter (private val context: Context?, private val notesList: RealmResults<Notes>)
    :RecyclerView.Adapter<RecyclerView.ViewHolder>()
{
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder
    {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.notes_rv_layout,parent,false)
        return ViewHolder(v)
    }

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

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

        holder.itemView.titleTV.text = notesList[position]!!.title
        holder.itemView.descTV.text = notesList[position]!!.description
        holder.itemView.idTV.text = notesList[position]!!.id.toString()

    }
    class ViewHolder(v: View?): RecyclerView.ViewHolder(v!!){
        val title = itemView.findViewById<TextView>(R.id.titleTV)
        val desc = itemView.findViewById<TextView>(R.id.descTV)
        val id = itemView.findViewById<TextView>(R.id.idTV)
    }

}
like image 262
Brett Hudson Avatar asked Jun 07 '26 10:06

Brett Hudson


2 Answers

Edit: Kotlin Android Extensions is now deprecated. See here.


You are either missing the Android KTX extensions, which require you to add them to the top of your app/build.gradle file like this:

apply plugin: 'kotlin-android-extensions'

and/or you are missing the import of your synthetic views at the top of this .kt file. When you are missing an import, you can put the cursor on the code that has the error, press Alt+Enter, and select the option to import the relevant class or function.

That said, synthetic views are kind of obsolete now that there is view binding. View binding makes it much easier to create ViewHolders because it caches all the view references for you. With view binding, your ViewHolder class would simply be:

class ViewHolder (val binding: NotesRvLayoutBinding): RecyclerView.ViewHolder(binding.root)

and onCreateViewHolder becomes:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder
    {
        val binding = NotesRvLayoutBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }

and onBindViewHolder becomes:

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

        holder.binding.titleTV.text = notesList[position].title
        holder.binding.descTV.text = notesList[position].description
        holder.binding.idTV.text = notesList[position].id.toString()

    }
like image 128
Tenfour04 Avatar answered Jun 10 '26 15:06

Tenfour04


You haven't imported it yet. Add these lines to your app's build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
like image 43
Sơn Phan Avatar answered Jun 10 '26 14:06

Sơn Phan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!