0

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

emkarachchi
  • 750
  • 1
  • 7
  • 18
admindunet
  • 21
  • 7

3 Answers3

1

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

martinseal1987
  • 1,862
  • 8
  • 44
  • 77
0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
emkarachchi
  • 750
  • 1
  • 7
  • 18
  • 1
    you should avoid this where possible, its not completely bad but the less you pass around context the better, if you need a context get it from the view like for instance in onCreateViewHolder(parent: ViewGroup, viewType: Int) you can get a context using parent.context – martinseal1987 Jan 02 '21 at 14:37
  • and using getActivity() should also be discouraged where possible as it can be null if called from a fragment again you can substitute this by using a view – martinseal1987 Jan 02 '21 at 14:42
  • @martinseal1987 I was doing this inside a fragment class. but I couldn't find it becoming `null`. But I wasn't aware of the `parent.context ` thing. Thanks. – emkarachchi Jan 02 '21 at 14:48
  • Yh I'm surprised the IDE isn't highlighting it to tell you this could be null – martinseal1987 Jan 02 '21 at 17:57
0

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

  • if i change Glide.with(this) to Glide.with(context) i get this error : Classifier 'Context' does not have a companion object, and thus must be initialized here – admindunet Jan 02 '21 at 15:01