Home > OS >  Can't find ItemView
Can't find ItemView

Time:10-27

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)
    }
  }
}

Is there in kotlin only MenuView.ItemView? Because Glide expects ItemView

Error Log:

Type mismatch: inferred type is MenuView.ItemView but ImageView was expected.

And if I paste instead of MenuView.ItemView ImageView then it says:

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)

CodePudding user response:

You should be using ImageView and not ItemView there.

For the ClassCastException check that the layout has id ivProfile for the ImageView only and not e.g. in a higher-level ConstraintLayout.

  • Related