android - 从库中获取资源ID

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述android - 从库中获取资源ID相关的知识,希望能为你提供帮助。
所以我有一个程序与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; } }

OnItemClick虚空中的三个Toast是我尝试显示id ...但仅显示图库中图像的位置。
还有其他解决方案吗?我已经读过关于动力学的东西......但是可以理解......
我应该把代码放在哪里以获得我想要的ID?
任何帮助?谢谢。
答案
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 int imageId = (Integer) (parent.getItemAtPosition(position)); myImageView.setImageResource(imageId); //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(); } });

在您的适配器中,getItem应返回数据集的对象
public Object getItem(int position) { return (mImageId[position]); }

另一答案你可以做这样的事情让你的image view上的qazxsw poi qazxsw poi作为qazxsw poi:
resource

【android - 从库中获取资源ID】这将返回图像作为所选图像视图资源的可绘制对象。

    推荐阅读