RecyclerView 适配器中的 findViewById returns null
findViewById returns null in RecyclerView Adapter
我正在创建一个回收器视图以及我对使用 findViewById 获取的图像和文本视图的引用 returns null。
我试过更改id,分别更改图像和文本,但无济于事,我一直在调试一整天
学生行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp">
<ImageView
android:id="@+id/itemImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/itemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recycling me"
android:textSize="15sp"/>
</LinearLayout>
适配器
class StudentListAdapter(val list:ArrayList<Student>, internal var newInstance: onItemClickListener) : RecyclerView.Adapter<StudentListAdapter.StudentViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.activity_student_list, parent, false)
return StudentViewHolder(view)
}
override fun getItemCount(): Int{
return list.size
}
override fun onBindViewHolder(holder:StudentViewHolder, position: Int) {
holder.title.text = list[position].name
holder.image.setImageResource(list[position].image)
// var student = list.get(position)
// holder.textView?.text = student.name
// holder.image.setImageResource(student.image)
holder.itemView.setOnClickListener {
newInstance.onItemClick(list[position])
}
}
class StudentViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
internal var title: TextView
internal var image:ImageView
init{
image = itemView.findViewById(R.id.itemImage)
title = itemView.findViewById(R.id.itemText)
}
}
你正在给你的 Activity
充气。也许您需要更改为 ViewHolder
?
val view = LayoutInflater.from(parent.context).inflate(R.layout.activity_student_list, parent, false)
它应该是
val view = LayoutInflater.from(parent.context).inflate(R.layout.student_row, parent, false)
您在 onCreateViewHolder
中使用了错误的 layout
。您没有使用 R.layout.student_row
布局,而是使用 R.layout.activity_student_list
.
尝试替换这个:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.student_row, parent, false)
return StudentViewHolder(view)
}
if (adapterPosition == RecyclerView.NO_POSITION) return
删除了这一行,它开始工作了!
我正在创建一个回收器视图以及我对使用 findViewById 获取的图像和文本视图的引用 returns null。
我试过更改id,分别更改图像和文本,但无济于事,我一直在调试一整天
学生行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp">
<ImageView
android:id="@+id/itemImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/itemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recycling me"
android:textSize="15sp"/>
</LinearLayout>
适配器
class StudentListAdapter(val list:ArrayList<Student>, internal var newInstance: onItemClickListener) : RecyclerView.Adapter<StudentListAdapter.StudentViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.activity_student_list, parent, false)
return StudentViewHolder(view)
}
override fun getItemCount(): Int{
return list.size
}
override fun onBindViewHolder(holder:StudentViewHolder, position: Int) {
holder.title.text = list[position].name
holder.image.setImageResource(list[position].image)
// var student = list.get(position)
// holder.textView?.text = student.name
// holder.image.setImageResource(student.image)
holder.itemView.setOnClickListener {
newInstance.onItemClick(list[position])
}
}
class StudentViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
internal var title: TextView
internal var image:ImageView
init{
image = itemView.findViewById(R.id.itemImage)
title = itemView.findViewById(R.id.itemText)
}
}
你正在给你的 Activity
充气。也许您需要更改为 ViewHolder
?
val view = LayoutInflater.from(parent.context).inflate(R.layout.activity_student_list, parent, false)
它应该是
val view = LayoutInflater.from(parent.context).inflate(R.layout.student_row, parent, false)
您在 onCreateViewHolder
中使用了错误的 layout
。您没有使用 R.layout.student_row
布局,而是使用 R.layout.activity_student_list
.
尝试替换这个:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.student_row, parent, false)
return StudentViewHolder(view)
}
if (adapterPosition == RecyclerView.NO_POSITION) return
删除了这一行,它开始工作了!