0

I am unable to attach the views from my xml file in onBindViewHolder of Recycler View in my Adapter. I'm getting the following error:

Unresolved reference: ivArticleImage

I am using DiffUtil Class.

I Can access them by Binding method but by normal i can't. SomeBody Plss help!! here is my XML file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:layout_margin="4dp">

    <ImageView
        android:id="@+id/ivArticleImage"
        android:layout_width="160dp"
        android:layout_height="90dp"
        android:scaleType="centerCrop"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

</androidx.constraintlayout.widget.ConstraintLayout>

My adapter class

class MyAdapter : RecyclerView.Adapter<MyAdapter.ArticleViewHolder>() {
    inner class ArticleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    private val differCallback = object : DiffUtil.ItemCallback<Article>() {
        override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
            return oldItem.url == newItem.url
        }

        override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
            return oldItem == newItem
        }

    }

    private val differ = AsyncListDiffer(this, differCallback)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
        return ArticleViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_article, parent, false)
        )
// item_artcile is my XMl file
    }

    override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
       val article = differ.currentList[position]
       holder.itemView.apply {
           Glide.with(this).load(article.urlToImage).into(ivArticleImage)// giving error

        }

    }

    override fun getItemCount(): Int {
       return differ.currentList.size
    }

}

'Article' is My data class..

I tried various method but error was still there only solution which i was able to find was to use binding method. but I am not using viewBinding in my project.

cutiko
  • 9,887
  • 3
  • 45
  • 59

2 Answers2

1

Your ArticleViewHolder class doesn't have anything to reach ivArticleImage because the itemView is just a View not a view binding. You have options:

Find the view

itemView.findViewById<ImageView>(R.id.ivArticleImage)

Expose the binding

 inner class ArticleViewHolder(val binding: YourBinding) : RecyclerView.ViewHolder(binding.root)

Please remember to update the inflation in onCreateViewHolder because now your constructor use a DataBinding:

val inflater = LayoutInflater.from(parent.context)
val binding = YourDataBinding.inflate(inflater, ...)
return ArticleViewHolder(binding)

And you can do what you want with a minimal change

holder.binding.apply {

       Glide.with(this).load(article.urlToImage).into(ivArticleImage)

}
cutiko
  • 9,887
  • 3
  • 45
  • 59
0

@cutiko's answer is correct if you are using view binding. If you are not using it, you can just extend your view holder like this:

inner class ArticleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val myImageView = itemView.findViewById<ImageView>(R.id.ivArticleImage)
}

Then later access your imageView:

override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
   val article = differ.currentList[position]
   Glide.with(holder.myImageView).load(article.urlToImage).into(holder.myImageView)
}