从Array填充GridView

时间:2014-09-03 19:05:37

标签: java android gridview

我有一系列图片网址,我想使用Picasso下载图片并在网格视图中显示它们。我当前的实现工作正常,但它将最后一个图像放入网格视图中的每个图像视图。

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 个答案:

答案 0 :(得分:1)

为每个项目调用一次getView(即调用它的次数等于'getCount')。最简单的方法是抛弃for循环并使用position参数查找thumbnailUrl。

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

答案 1 :(得分:1)

您的getView实际上是将每个图片加载到列表项中,因此只有最后一个图像成为可见的图像!

正确的解决方案如下:

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

您还应该考虑使用ViewHolder模式(http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder),这样每次调用getView时都不会执行findViewById!

答案 2 :(得分:0)

试试这个

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