如何显示来自 url with holder 的图像视图
How to display an imageview from a url with holder
从 url 在 Imageview 上显示图像我使用了这段代码,但没有成功:
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)
}
我有这个错误:
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
如何解决这个问题并正确显示图像?谢谢
很可能你将它传递给Glide.with的地方(this)可能引用了class并且glide没有接受它的构造函数,给它一个像[=12这样的视图=](holder.yourView) 这是给它一个应该修复它的视图
以下是我如何使用 java 实现相同的行为。
创建新的Adapter
对象时,需要传递Activity Context.
myRecyclerViewAdapter = new MyAdapter(dataList,getActivity());
在您的适配器中创建一个 Context
变量 class
private final Context context;
然后在constructor
的Adapter
class
public MyAdapter(List<MyData> dataSet, Context context)
{
this.dataSet = dataSet;
this.context = context; //this line should be added
}
终于可以加载图片了,如下图
Glide.with(context).load(imageUrl).into(holder.imageView);
而不是 Glide.with(this),在适配器的构造函数中提供关联的 UI 上下文并使用:Glide.with(context)
从 url 在 Imageview 上显示图像我使用了这段代码,但没有成功:
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)
}
我有这个错误:
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
如何解决这个问题并正确显示图像?谢谢
很可能你将它传递给Glide.with的地方(this)可能引用了class并且glide没有接受它的构造函数,给它一个像[=12这样的视图=](holder.yourView) 这是给它一个应该修复它的视图
以下是我如何使用 java 实现相同的行为。
创建新的Adapter
对象时,需要传递Activity Context.
myRecyclerViewAdapter = new MyAdapter(dataList,getActivity());
在您的适配器中创建一个 Context
变量 class
private final Context context;
然后在constructor
的Adapter
class
public MyAdapter(List<MyData> dataSet, Context context)
{
this.dataSet = dataSet;
this.context = context; //this line should be added
}
终于可以加载图片了,如下图
Glide.with(context).load(imageUrl).into(holder.imageView);
而不是 Glide.with(this),在适配器的构造函数中提供关联的 UI 上下文并使用:Glide.with(context)