I was writing a recyclerview by using view binding features, but I am not be able to access the id of the item which is existing in the item.xml file in the project. Here is codes of adapter and item.xml file.
adapter:
class RecyclerviewAdapter(val Data:List<Data>):RecyclerView.Adapter<RecyclerviewAdapter.ViewHolder>() {
class ViewHolder(binding: ItemBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemBinding.inflate(inflater, parent, false)
return ViewHolder(binding)
}
override fun getItemCount(): Int {
return Data.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.
}
}
item.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="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/date"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/temperaturec"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/temperaturef"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/summary"/>
</LinearLayout>
I want to access the id of texts.
You need to make the binding
constructor parameter into a property using val
:
class ViewHolder(val binding: ItemBinding) : RecyclerView.ViewHolder(binding.root)
Then you can use it to access the views:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.date.text = /* for example: */ Data[position].date
}
You don't need the actual ID of the view that you asked for since you're using view binding.