将图像放入RecyclerView。

huangapple go评论83阅读模式
英文:

Place image in a RecyclerView

问题

        public void onBindViewHolder(@NonNull ViewHolderProducto holder, int position) {
            holder.nombre.setText(listProductos.get(position).getNombre());
            holder.descrip.setText(listProductos.get(position).getDescripcion());
            holder.valor.setText(listProductos.get(position).getValor());

            // Implement Uri.parse for setting image URI
            String imageUriString = listProductos.get(position).getImage();
            Uri imageUri = Uri.parse(imageUriString);
            holder.imagen.setImageURI(imageUri);
        }
英文:

I have a RecyclerView with its respective adapter which is composed of a layout with an ImageView and some TextView, when I pass the data list I get the image that I must place as a URI in String, I don't know how to pass the String of the image to Uri for the ImageView:

public class AdapterProductos extends RecyclerView.Adapter<AdapterProductos.ViewHolderProducto> {

    ArrayList<Producto> listProductos;

    public AdapterProductos(ArrayList<Producto> listProductos) {
        this.listProductos = listProductos;
    }

    //CONECTAR EL LAYOUT A EL ADAPTADOR
    @NonNull
    @Override
    public ViewHolderProducto onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_list_producto, null, false);
        return new ViewHolderProducto(view);
    }

    //OBTENER LOS DATOS
    @Override
    public void onBindViewHolder(@NonNull ViewHolderProducto holder, int position) {
        holder.nombre.setText(listProductos.get(position).getNombre());
        holder.descrip.setText(listProductos.get(position).getDescripcion());
        holder.valor.setText(listProductos.get(position).getValor());
        //holder.imagen.setImageURI(listProductos.get(position).getImagen());
    }

    //OBTENER EL TAMAÑO DE LA LISTA
    @Override
    public int getItemCount() {
        return listProductos.size();
    }

    //ENLAZAR LOS ELEMENTOS VISUALES
    public class ViewHolderProducto extends RecyclerView.ViewHolder {

        TextView nombre, descrip, valor;
        ImageView imagen;
        public ViewHolderProducto(@NonNull View itemView) {
            super(itemView);
            imagen = itemView.findViewById(R.id.imagen_itemProducto);
            nombre = itemView.findViewById(R.id.nombre_itemProducto);
            descrip = itemView.findViewById(R.id.descrip_itemProducto);
            valor = itemView.findViewById(R.id.valor_itemProducto);
        }
    }
}

Editado:
How can I implement the Uri.parse (image) in this part?:

    public void onBindViewHolder(@NonNull ViewHolderProducto holder, int position) {
    holder.nombre.setText(listProductos.get(position).getNombre());
    holder.descrip.setText(listProductos.get(position).getDescripcion());
    holder.valor.setText(listProductos.get(position).getValor());
}

Because to get the image I have to write:

holder.imagen.setImageURI (listProductos.get (position) .getImage ());

答案1

得分: 0

我个人会推荐你使用Glide库(链接)。
它是一个强大的图片加载工具,而且使用起来非常简便。
要按你的需求加载图片,你只需要写下这段代码:

Glide
.with(context)
.load(url)
.into(imagen);
英文:

I personally would recomend you to use Glide library(link).
It is a powerful tool for image loading that is also pretty easy to use.
To load the image as you want, you just need to write this:

Glide
.with(context)
.load(url)
.into(imagen);

答案2

得分: 0

当您想要添加图像URI时,请使用静态方法 `Uri.parse("您的图像URI")`
当您想要添加图像URL时,您可以在Android中使用Glide或Picasso库
Picasso:

    Picasso.get()
      .load(url)
      .resize(50, 50)
      .centerCrop()
      .into(imageView)

Glide:

    Glide
    .with(context)
    .load(url)
    .into(imageView);

在使用之前不要忘记将库添加到依赖中。

根据我的经验,我更喜欢Glide,它非常快速,并由Google开发。

在dependencies中添加Picasso库:

    dependencies {
        implementation 'com.squareup.picasso:picasso:2.71828' // Picasso库

要添加Glide库,请使用以下设置:

在repositories中:

     repositories {
          google()
          jcenter()
        }
    
在dependencies中:

       dependencies {
          implementation 'com.github.bumptech.glide:glide:4.11.0'
          annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
        }
英文:

when you want to add image URI use static method Uri.parse("your image uri")
when you want to add image URL you can use glide or picasso library in android
Picasso :

Picasso.get()
  .load(url)
  .resize(50, 50)
  .centerCrop()
  .into(imageView)

glide :

Glide
.with(context)
.load(url)
.into(imagen);

Don't forget to add the library in dependency before use it.

From my experience I prefer glide it so fast and developed by google

dependencies {
 implementation 'com.squareup.picasso:picasso:2.71828' // Picasso library

this to add glide library

 repositories {
      google()
      jcenter()
    }

   dependencies {
      implementation 'com.github.bumptech.`enter code here`glide:glide:4.11.0'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    }

huangapple
  • 本文由 发表于 2020年10月5日 02:08:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64198169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定