gpt4 book ai didi

java - 从数组填充 GridView

转载 作者:行者123 更新时间:2023-12-02 05:30:00 25 4
gpt4 key购买 nike

我有一组图像网址,我想使用 Picasso 下载图像并在 GridView 中显示它们。我当前的实现有效,但它将最后一个图像放入 GridView 中的每个 ImageView 中。

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

最佳答案

getView 对每个项目调用一次(即调用次数等于“getCount”)。最简单的方法是放弃 for 循环并使用位置参数查找缩略图 Url。

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

关于java - 从数组填充 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com