'itemview in Adapter for recyclerview not getting id from xml
this is my class i've been tryinng to get the imageviewcard which is an xml id un the cardview, and no matter if i do it with findviewbyid or just itemview. it is not catching it, i'm so desperate!!!
class CustomAdapter (
private val list: List<DataCard>
): RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
val items: MutableList<CardView>
init{
this.items = ArrayList()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.cardviews,parent,false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
val item: DataCard = list[position]
holder.bind(item)
// holder.itemView.findViewById<TextView>(R.id.ettitle)
// holder.itemView.findViewById<ImageView>(R.id.cardimage)
}
override fun getItemCount(): Int { return list.size }
inner class ViewHolder
internal constructor(
itemView: View
) : RecyclerView.ViewHolder(itemView){
// val ettitlecard: TextView = itemView.imageviewcard
// val card: LinearLayout?= itemView.findViewById(R.id.cardview)
fun bind(item: DataCard) {
//THIS JUST WON'T WORK NOR WITH FINVIEWBYID
// val ettitlecard: TextView = itemView.ettitlecard
// val card: LinearLayout?= itemView.card
}
val binding = CardviewsBinding.bind(itemView)
}
}
Solution 1:[1]
could you share your layout.xml and the error you are getting to analyse your problem.
I think you can simplify your ViewHolder creation:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = CardviewsBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return ViewHolder(binding)
}
class ViewHolder(private val binding: CardviewsBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(item: DataCard) {
// use binding here to populate view
}
}
Maybe this also fixes your problem.
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 | a_local_nobody |
