有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从数组填充GridView

我有一个图像URL数组,我想使用毕加索下载图像并在网格视图中显示它们。我当前的实现工作正常,但它将最后一个图像放入网格视图中的每个图像视图中

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

  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. # 2 楼答案

    试试这个

    // 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);
    
  3. # 3 楼答案

    每个项目调用一次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;
        }