我正在使用Picasso在我的Android应用中设置gridview。我想做的是基本上按下gridview中的图像并转到详细视图。
数据填充在String数组中,如下所示:
static final String[] URLS = {
"http://i.imgur.com/file1.jpg", "http://i.imgur.com/file2.jpg",
"http://i.imgur.com/file3.jpg", "http://i.imgur.com/file4.jpg"
};
在ImageAdapter
中,这些URL将被放入String ArrayList:
public class ImageAdapter extends BaseAdapter {
private final Context context;
private final List<String> urls = new ArrayList<String>();
public ImageAdapter(Context context) {
this.context = context;
// Ensure we get a different ordering of images on each run.
Collections.addAll(urls, Data.URLS);
//Collections.shuffle(urls);
// Triple up the list.
ArrayList<String> copy = new ArrayList<String>(urls);
urls.addAll(copy);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
view.setScaleType(CENTER_CROP);
}
// 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()
.centerCrop()//
.into(view);
return view;
}
@Override public int getCount() {
return urls.size();
}
@Override public String getItem(int position) {
return urls.get(position);
}
@Override public long getItemId(int position) {
return position;
}
}
问题是当我想获取Item的ID时,它全部存储在String中。以下是我的详细信息视图活动的样子:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter ia = new ImageAdapter(this);
ImageView iv = (ImageView) findViewById(R.id.full_image_view);
iv.setImageResource(ia.getItemId(position));
}
position
来自我的主要活动,它位于整数中。当我想放入getItemId时,setImageResource
会抛出此错误:
ImageView类型中的方法setImageResource(int)不是 适用于参数(长)
你们有任何想法如何解决这个问题吗?
编辑:添加了ImageAdapter类代码
答案 0 :(得分:2)
您仍然需要在详细信息视图中调用Picasso并从适配器传递URL,而不是ID。
Picasso.with(context) //
.load(ia.getItem(position)) //
.placeholder(R.drawable.placeholder) //
.error(R.drawable.error) //
.fit()
.centerCrop()//
.into(iv);