I'm trying to learn mobile development and got stuck trying to do a recyclerview.
I've set up the data class and also the xml file of the item.
When I try to bind the data to the xml file, in the onBindViewHolder function, the system gives me no option to select which ID I want to bind to.
Data Class
package com.example.recyclerview1
data class Todo(
val title: String,
var isChecked: Boolean
)
XML item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="16dp">
<TextView
android:id="@+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="title"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cbDone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/cbDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter Class
package com.example.recyclerview1
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class TodoAdapter(
var todos:List<Todo>
): RecyclerView.Adapter<TodoAdapter.TodoViewHolder>() {
inner class TodoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_todo,parent,false)
return TodoViewHolder(view)
}
override fun getItemCount(): Int { return todos.size}
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.itemView.tvTitle << right here I want to select the ID of the TextView, but the IDE does not show me
}
}
You can use the findViewById
method for getting an instance of the view component and then perform some operation on them like this:
class TodoAdapter(var todos:List<Todo>) : RecyclerView.Adapter<TodoAdapter.TodoViewHolder>() {
inner class TodoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
{
var tvTitle: TextView = itemView.findViewById(R.id.tvTitle)
var cbDone: CheckBox = itemView.findViewById(R.id.cbDone)
}
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.tvTitle. // DO YOUR STUFF HERE
}
}
Or you can use ViewBinding
capabilities for accessing view instances like is written in this post.
If you want to perform the whole data binding inside XML, then you need to use DataBinding
feature. To do that, just follow this post on how to enable DataBinding and how to use it inside your project.