'holder.itemView.rowLayout.setOnClickListener
I have created the following adapter class for recyclerview but there is a compilation error while building in layout section "holder.itemView.rowLayout.setOnClickListener" Unresolved reference: rowLayout, I have changed the layout to LinearLayout aswell but still there is compilation error The following is the Adapter class and customRow.XML respectively,
package com.example.roomapp.fragments.list
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.example.roomapp.R
import com.example.roomapp.model.User
import kotlinx.android.synthetic.main.custom_row.view.*
class ListAdapter: RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
private var userList = emptyList<User>()
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_row, parent, false))
}
override fun getItemCount(): Int {
return userList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = userList[position]
holder.itemView.id_txt.text = currentItem.id.toString()
holder.itemView.txfirstName.text = currentItem.firstName
holder.itemView.txlastName.text = currentItem.lastName
holder.itemView.txContactNo.text = currentItem.contactNumber.toString()
holder.itemView.txEmailAddress.text = currentItem.email.toString()
holder.itemView.rowLayout.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
}
fun setData(user: List<User>){
this.userList = user
notifyDataSetChanged()
}
}
customRow.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="5dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/id_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imProd"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/txfirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="Samsung Note 9"
android:textColor="@color/purple_700"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txlastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="Rs. 98,000"
android:textColor="@color/purple_700"
android:textSize="14dp" />
<TextView
android:id="@+id/txContactNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="446661122"
android:textColor="@color/purple_700"
android:textSize="13dp" />
<TextView
android:id="@+id/txEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="[email protected]"
android:textColor="@color/purple_700"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Solution 1:[1]
There is no rowLayout in the xml.
But if you want an onclick on entire row, you can change
holder.itemView.rowLayout.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
to
holder.itemView.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arkaprabha Mahata |
