raginggoat
raginggoat

Reputation: 3600

Populate GridView from Array

I have an array of image urls and I want to download the images using Picasso and show them in a grid view. My current implementation works but it is putting the last image into every image view in the grid view.

public class GridViewAdapter extends ArrayAdapter {

        Context context;

        public GridViewAdapter(Context context) {
            super(context, 0);
            this.context = context;
        }

        public int getCount() {
            return thumbnailURLS.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View row = convertView;

            if(row == null) {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(R.layout.grid_row, parent, false);

                ImageView gridImageView = (ImageView)row.findViewById(R.id.gridImageView);

                for(String s : thumbnailURLS) {
                    Picasso.with(context)
                            .load(s)
                            .placeholder(R.drawable.placeholder)
                            .error(R.drawable.placeholder)
                            .into(gridImageView);
                }
            }

            return row;
        }
    }

Upvotes: 0

Views: 450

Answers (3)

Davide
Davide

Reputation: 614

try this

// Get the image URL for the current position.
String url = getItem(position);

// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context) //
    .load(url) //
    .placeholder(R.drawable.placeholder) //
    .error(R.drawable.error) //
    .fit() //
    .into(view);

Upvotes: 0

Nyx
Nyx

Reputation: 2243

Your getView is actually loading every single image into the list item, and so only the last one becomes the visible one!

The correct solution would be as follows:

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    if (row == null) {
      LayoutInflater inflater = ((Activity) context).getLayoutInflater();
      row = inflater.inflate(R.layout.grid_row, parent, false);
    }

    ImageView gridImageView = (ImageView) row.findViewById(R.id.gridImageView);
    Picasso.with(context).load(thumbnailURLS.get(position)).placeholder(R.drawable.placeholder)
        .error(R.drawable.placeholder).into(gridImageView);
    return row;
  }

You should also look into using the ViewHolder pattern (http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder) such that you are not doing a findViewById every time getView is being called!

Upvotes: 1

James Baxter
James Baxter

Reputation: 1246

getView is called once for each item (ie the number of times it is called is equal to 'getCount'). Easiest thing to do would be to ditch the for loop and lookup the thumbnailUrl using the position argument.

ie

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;

        if(row == null) {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(R.layout.grid_row, parent, false);
        }

        ImageView gridImageView = (ImageView) row.findViewById(R.id.gridImageView);

        Picasso.with(context)
               .load(thumbnailURLs.get(position))
               .placeholder(R.drawable.placeholder)
               .error(R.drawable.placeholder)
               .into(gridImageView);

        return row;
    }

Upvotes: 1

Related Questions