0

所以我有一个程序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?

有什么帮助吗?谢谢。

4

2 回答 2

1
 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]);
 }
于 2013-05-13T08:35:07.740 回答
0

你可以做这样的事情来获得image view resourcedrawableitemClickListener

(ImageView)view.getBackground()

这会将图像作为选定图像视图资源的可绘制对象返回。

于 2013-05-13T08:35:10.523 回答