admindunet
admindunet

Reputation: 21

How to display an imageview from a url with holder

to display the images on the Imageview from an url I use this code, but without success :

class Article(var id:Int,var nom:String,var lienimg:String, var ifram:String){
}

class ArticleAdapter (var articles:ArrayList<Article>) : RecyclerView.Adapter<ArticleAdapter.MyViewHolder>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        var vue=LayoutInflater.from(parent.context).inflate(R.layout.listeviss, parent, false)
        return MyViewHolder(vue)
    }
    override fun getItemCount(): Int {
        return articles.size
    }
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        var article = articles.get(position)
        holder.nomvisite.setText(article.nom)
        holder.lieimgvisite.setText(article.lienimg)
        holder.ifram.setText(article.ifram)


        var urldelimg:String = "linkmysite.com/image.jpg"
        Glide.with(this).load(urldelimg).into(holder.imagedubloc)

    }

    class MyViewHolder(var vue:View):RecyclerView.ViewHolder(vue){
        var nomvisite=vue.findViewById<TextView>(R.id.nom_visitevirtuelle)
        var lieimgvisite=vue.findViewById<TextView>(R.id.lienimg)
        var ifram=vue.findViewById<TextView>(R.id.ifram)
        var imagedubloc=vue.findViewById<ImageView>(R.id.imagedubloc)

    }

i have this error :

None of the following functions can be called with the arguments supplied: 
public open fun with(p0: Activity): RequestManager defined in com.bumptech.glide.Glide
public open fun with(p0: android.app.Fragment): RequestManager defined in com.bumptech.glide.Glide
public open fun with(p0: Context): RequestManager defined in com.bumptech.glide.Glide
public open fun with(p0: View): RequestManager defined in com.bumptech.glide.Glide
public open fun with(p0: androidx.fragment.app.Fragment): RequestManager defined in com.bumptech.glide.Glide
public open fun with(p0: FragmentActivity): RequestManager defined in com.bumptech.glide.Glide

How can I fix this and display the image without error ? Thank You

Upvotes: 0

Views: 420

Answers (3)

martinseal1987
martinseal1987

Reputation: 2428

Most likely where you've passed this to Glide.with(this) probably references the class and glide doesn't have a constructor that takes that, give it a view like Glide.with(holder.yourView) this is giving it a view which should fix it

Upvotes: 1

emkarachchi
emkarachchi

Reputation: 802

Here is how I've achieved the same behavior using java.

When creating a new Adapter object, you need to pass the Activity Context.

myRecyclerViewAdapter = new MyAdapter(dataList,getActivity());

create a Context variable in your adapter class

private final Context context;

Then in the constructor of the Adapter class

public MyAdapter(List<MyData> dataSet, Context context)
{
    this.dataSet = dataSet;
    this.context = context; //this line should be added
}

Finally, you can load the image as follows.

Glide.with(context).load(imageUrl).into(holder.imageView);

Upvotes: 0

harpal Singh
harpal Singh

Reputation: 1

Instead of Glide.with(this), provide associated UI context in adapter's constructor and use like: Glide.with(context)

Upvotes: 0

Related Questions