我正在使用 Picasso 在我的 Android 应用程序中设置网格视图。我想做的基本上是在网格视图中按下图像并转到详细视图。
数据填充在字符串数组中,如下所示:
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;
}
}
问题是当我想获取项目的 ID 时,它都存储在字符串中。这是我的详细信息视图活动的样子:
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) 不适用于参数(long)
你们知道如何解决这个问题吗?
编辑:添加 ImageAdapter 类代码