mchd
mchd

Reputation: 3163

Picasso throws "unresolved reference" for my imageView but it has been declared in the ViewHolder

I'm trying to use Picasso to load my RecyclerView with JSON images but the issue is that Picasso does not recognize my imageView, despite the fact that it was declared in the ViewHolder.

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.list_item.view.*

class PosterAdapter(val movieData: Movies) : RecyclerView.Adapter<PosterHolder>(){
    val movieList = mutableListOf<Movies>()

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PosterHolder{

        val layoutInflater = LayoutInflater.from(parent?.context)
        val listItem = layoutInflater.inflate(R.layout.list_item, parent, false)
        return PosterHolder(listItem)
    }

    override fun onBindViewHolder(holder: PosterHolder, position: Int) {
        Picasso
                .get()
                .load("" + R.string.base_URL + "" + movieData.moviePoster)
                .into(PosterHolder.imageView)//identifier imageView is red

        holder.view.movie_poster?.scaleType = ImageView.ScaleType.FIT_CENTER
    }
}

class PosterHolder(val view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
    var imageView: ImageView? = null

    fun PosterHolder(view: View){ this.imageView = view.findViewById<View>(R.id.movie_poster) as ImageView }

    override fun onClick(p0: View?) {}
}

Upvotes: 0

Views: 1351

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21053

There is two problem in your code. First problem is you are passing a resource id in url as R.string.base_URL . You should read the resource and pass the value of it not id . Declare Base url as global.

private var BASE_URL: String?=null

Get the context in Adapter .

BASE_URL=context.resources.getString(R.string.base_URL)

Or you can pass BASE_URL itself in constructor .

Second problem is you should use holder.imageView instead of PosterHolder.imageView

Picasso
            .get()
            .load(BASE_URL + movieData.moviePoster)
            .into(holder.imageView)

Upvotes: 3

Related Questions