3

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)
    }

}
Brett Hudson
  • 51
  • 1
  • 7

4 Answers4

4

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()

    }
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
1

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'
Sơn Phan
  • 1,120
  • 6
  • 12
1

Please use this.

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

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

    }

instead of this.

 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()

    }

Remove itemView from onBindViewHolder method.

Pradeep Kumar
  • 586
  • 3
  • 19
0

There is no extension function import.Without that extension you can't reference the id directly.

If you want to reference directly then import android-extension in your build gradle

Sathyan
  • 138
  • 1
  • 10