Home > database >  Unresolved reference: userID in Kotlin, possible issue with data binding
Unresolved reference: userID in Kotlin, possible issue with data binding

Time:11-16

I have been looking at other similar posts on the topic, but as I am deeply learning Kotlin at the moment I'd like to discuss the problem, the solution, and why it happened if possible.

I am getting the following error:

C:\Users\Paul\Documents\Projects\DataApp\app\src\main\java\com\example\dataapp\MyAdapter.kt: (19, 31): Unresolved reference: userId

Here is a sample of the code, the issue is happening in the ViewHolder class:

package com.example.dataapp

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

class MyAdapter (val context: Context, val userList: List<MyDataItem>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        var userId: TextView
        var title: TextView

        init {
            userId = itemView.userId
            title = itemView.title
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var itemView = LayoutInflater.from(context).inflate(R.layout.row_items, parent, false)
        return ViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.userId.text = userList[position].userId.toString()
        holder.title.text = userList[position].title
    }

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

}

In the XML I have set two text fields with id's of userId and title:

Text

But I am getting this unresolved error for both. I am still working on my knowledge in Kotlin and Android and am very new, so no doubt will be something very basic.

I have so far:

Double checked the syntax matches Checked I am binding correctly - I believe this is where the issue lies. I am following a tutorial that is not using binding and is just referencing using 'R.layout.activity_main' however I am using binding.root - could this be the issue?

Here is a link to the GitHub repo - https://github.com/Code4Wyatt/FetchDataKotlin

Thank you for any help! Please let me know if any more info is needed.

CodePudding user response:

You are not using viewbinding in this class.

itemView is just a View like any, it doesn't have fields called userId or title

You can probably do this for example to fix the error.

        init {
            userId = itemView.findViewById<TextView>(R.id.userId)
            title = itemView.findViewById<TextView>(R.id.title)
        }

Or to use viewbinding instead, change the onCreateViewHolder to

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    var itemView = RowItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return ViewHolder(itemView)
}

And change the ViewHolder to

class ViewHolder(itemView: RowItemsBinding): RecyclerView.ViewHolder(itemView.root) {
    var userId: TextView
    var title: TextView

    init {
        userId = itemView.userId
        title = itemView.title
    }
}

CodePudding user response:

You have DataBinding/ViewBinding enabled but you are not actually using it.

buildFeatures {
    dataBinding true
    viewBinding true
}

You are "normally" inflating the view:

LayoutInflater.from(context).inflate(R.layout.row_items, parent, false)

If you wish to use ViewBinding for this you need to inflate it with it.

RowItemBinding.inflate(LayoutInflater.from(context), parent, false)

and pass that instance to the ViewHolder so it can see it as ViewBinding

class ViewHolder(itemView: RowItemsBinding) : RecyclerView.ViewHolder(itemView.root)
  • Related