1
2
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tvName: TextView = itemView.findViewById(R.id.tvName)
var tvAge: TextView = itemView.findViewById(R.id.tvAge)
var ivProfile: ItemView = itemView.findViewById(R.id.ivProfile)
fun bind(contact: Contact) {
tvName.text = contact.name
tvAge.text = "Age: ${contact.age}"
Glide.with(context).load(contact.imageUrl).into (ivProfile)
}
}
}
科特林只有MenuView.ItemView
吗?因为Glide期望ItemView
错误日志:
Type mismatch: inferred type is MenuView.ItemView but ImageView was expected.
如果我粘贴而不是MenuView.ItemView
ImageView
,那么它会说:
java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.ImageView
at com.jakob.kotlinrecyclerview.ContactAdapter$ViewHolder.<init>(ContactAdapter.kt:35)
at com.jakob.kotlinrecyclerview.ContactAdapter.onCreateViewHolder(ContactAdapter.kt:17)
at com.jakob.kotlinrecyclerview.ContactAdapter.onCreateViewHolder(ContactAdapter.kt:13)
您应该使用ImageView
,而不是ItemView
。
对于ClassCastException
,检查布局是否具有仅用于ImageView
的idivProfile
,而不是例如在更高级别的ConstraintLayout
中。