Recyclerview在Layout中找不到元素的ID



我的RecyclerView找不到元素的id。我试着使用findViewById,但它不起作用,因为我的应用程序崩溃了。

这是我的自定义适配器-MyRecyclerAdapter.kt:

class MyRecyclerAdapter(
private var notiList : ArrayList<NotificationClass>) :RecyclerView.Adapter<MyRecyclerAdapter.RecyclerViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_layout,parent,false)
return RecyclerViewHolder(itemView)}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
//Problem Arise here
holder.itemView.titleView.text = notiList[position].title }
override fun getItemCount(): Int = notiList.size
class RecyclerViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)
}

这是我的物品布局-item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="30dp"
android:layout_margin="5dp"
app:cardCornerRadius="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/titleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="25sp"
android:textColor="@color/black"
android:fontFamily="@font/raleway_bold"
android:maxLines="1"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp" />
<TextView
android:id="@+id/descView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="20sp"
android:textColor="@color/black"
android:fontFamily="@font/raleway_semibold"
android:maxLines="3"
android:scrollbars="vertical"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>

您尚未为RecyclerView定义ViewHolder。应该如下所示:

class MyRecyclerAdapter(
private var notiList : ArrayList<NotificationClass>) :RecyclerView.Adapter<MyRecyclerAdapter.RecyclerViewHolder>(){

// Define below ViewHolder
internal inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var titleView: TextView = view.findViewById(R.id.titleView)
var descView: TextView = view.findViewById(R.id.descView)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_layout,parent,false)
return RecyclerViewHolder(itemView)}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
//Problem Arise here
holder.itemView.titleView.text = notiList[position].title }
override fun getItemCount(): Int = notiList.size
class RecyclerViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)
}

尽量不要从android.R访问。相反,试试这样的东西:

CategoryAdapterHolder(LayoutInflater.from(parent.context)
.inflate(com.example.secondhand.R.layout.category_list, parent, false))

最新更新