I am following a tutorial online due to this being my first time programming in kotlin, i just wanted to bind the text that i will get from a table in database to a ViewModel, i started with an example i saw online as a base:
in this code i am getting an error on the "itemView.userName.text = "test"", it does not recognise the 'userName' and i have no idea why.
With this Adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.timetally.R
import com.example.timetally.repository.entity.TaskType
class TaskTypeAdapter : RecyclerView.Adapter<TaskTypeAdapter.ViewHolder>() {
private var taskTypeList = emptyList<TaskType>()
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(taskType: TaskType) {
itemView.userName.text = "test"
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.task_type_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(taskTypeList[position])
}
override fun getItemCount(): Int = taskTypeList.size
fun setData(userLtaskTypeListist: List<TaskType>) {
this.taskTypeList = taskTypeList
notifyDataSetChanged()
}
}
And using this xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/userEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
<TextView
android:id="@+id/userPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
<TextView
android:id="@+id/userWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
I have tried a simpler layouts with just the one textView, clean and rebuild the project but i can't make it work
This is not the way to use the view binding in android. First of all in your module's gradle you have to enable the view binding feature:
android {
...
buildFeatures {
viewBinding = true
}
}
After that in your view holder you can create the binding instance and use it:
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val binding = TaskTypeLayoutBinding.bind(itemView)
fun bind(taskType: TaskType) {
binding.userName.text = "test"
}
}
When you enable the view binding feature, for all your layouts a binding class is generated automatically.