How to make a list of images using picasso?

1

I'm trying to upload a list of images that are on a server using picasso.

In the arrayy I only have the value of the id of the image:

"List<integer> myList = new ArrayList<integer>".
Ex: 1, 2, 3, 4, 5, 6;

first try just populating the list using the id values:

ArrayAdapter<Integer> arrayAdapter = new ArrayAdapter<Integer> 
(MainActivity.this, R.layout.list_item, R.id.item_title, myList);
            simpleList.setAdapter(arrayAdapter);

and it works fine, the list shows all the id.

But how can I do this using picasso? I know I can upload an image using picaso with the following format.

URL=" link goes the id that is in the array -"

Picasso.with (context) .load (URL) .into (imageView); but I would like to know if there is any way to load all at once.

    
asked by Pedro 06.07.2018 в 00:09
source

1 answer

1

For this I suggest you create a% custom Adapter , first add the dependency to use Picasso , this within your build.gradle :

dependencies {
    ...
    implementation 'com.squareup.picasso:picasso:2.71828'
    ...
}

now your Adapter would use the string "http://sample.com?id=" to complete the url of each image and upload using Picasso the image within ImageView :

Picasso.get().load("http://sample.com?id=" + elements.get(position)).into(holder.imageView);

Your Adapter would be:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class Adaptercito extends ArrayAdapter<Integer> {

    private final Activity _context;
    private final ArrayList<Integer> elements;

    public class ViewHolder {
        TextView textView;
        ImageView imageView;
    }

    public Adaptercito(Activity context, ArrayList<Integer> elements){
        super(context,R.layout.list_item, R.id.item_title , elements);
        this._context = context;
        this.elements = elements;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;

        if(convertView == null) {
            LayoutInflater inflater = _context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.list_item,parent,false);

            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.item_title);
            holder.imageView = (ImageView)convertView.findViewById(R.id.item_image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText("Imagen: "+elements.get(position));

        //* Carga url de imagen en ImageView   
        Picasso.get().load("http://sample.com?id=" +elements.get(position)).into(holder.imageView);

        return convertView;
    }
}

the Adapter would use a layout that must contain a ImageView to display the image, list_item.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">
<TextView
    android:id="@+id/item_title"
    android:layout_width="0dp"
    android:layout_height="match_parent"
        android:gravity="center_vertical"
    android:minHeight="20dp"
    android:padding="8dp"
    android:textStyle="bold"
    android:textColor="#000"
    android:layout_weight="0.5"/>
<ImageView
    android:id="@+id/item_image"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:scaleType="centerInside"
    android:layout_weight="0.5"/>
</LinearLayout>

In this way set the Adapter in your ListView :

Adaptercito arrayAdapter = new Adaptercito(this, (ArrayList<Integer>) myList);
simpleList.setAdapter(arrayAdapter);

To obtain a list of images similar to:

    
answered by 06.07.2018 / 03:01
source