0

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

3 Answers 3

1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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!

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.