所以我有一个程序Gallery
(用资源文件夹中的图片制作),一个按钮和一个ImageView
我想从图库中选择一张图像,按下按钮,然后更改ImageView
位图以显示从图库中选择的一张...
我已经修复了从活动到活动的变化方法......但我只是无法获取所选择的图像的id 、整数、可绘制、长、字符串......或任何有帮助的......
这是代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//CODE TO KNOW IF PICKED THE 1ST OR 2ND IMAGE
//Toast.makeText(Galeria.this, v.getId() + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, parent.getItemIdAtPosition(position) + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, id + "", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageId = {
R.drawable.image1, //first image
R.drawable.image2, //second image
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount()
return mImageId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageId[position]);
i.setLayoutParams(new Gallery.LayoutParams(400, 225));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
虚空中的三个 ToastOnItemClick
是我尝试显示身份的尝试……但仅显示画廊中图像的位置。
还有其他解决方案吗?我读过一些关于动态ID的东西......但可以很好地理解......
我应该把代码放在哪里以获得我想要的 id?
有什么帮助吗?谢谢。