0

我正在尝试从 Gallery Widget 加载选定的图像并将其加载到另一个活动中,我将非常感谢帮助!

基本上它是关于拍摄我在画廊中选择的图像并在OnItemClick意图中制作,或者我不知道在另一个活动中查看图像。

该类如下所示:

public class Motos extends Activity implements OnClickListener, OnItemSelectedListener, OnItemClickListener{

    Gallery g;

Integer[] images = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4 };


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.moto);


    g = (Gallery) findViewById(R.id.gallery1);
    g.setOnItemClickListener(this);
    g.setSpacing(2);


}

public class ImageAdapter extends BaseAdapter {

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return images.length;
    }

    public Object getItem(int position) {
        return images[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView i = new ImageView(mContext);

        i.setImageResource(images[position]);
        i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        //i.setLayoutParams(new Gallery.LayoutParams(136, 88));
        return i;
    }

    private Context mContext;

}


@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
    // TODO Auto-generated method stub

}

}

4

1 回答 1

0

好吧,看起来您可以通过执行以下操作来获取所选图像的资源 ID:

@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
    int selectedId = images[position];  
}

您可以将选定的资源 id 传递给另一个Activity,如下所示:

Intent intent = new Intent(Vehiculos.this, OtherActivity.class);
intent.putExtra("imageId", selectedId);
startActivity(intent);

然后像这样在你的另一个中检索 id Activity

Intent intent = getIntent();
int imageId = intent.getIntExtra("imageId", -1); // -1 would be a default value

这是一个粗略的例子——您可能需要添加一些错误检查。

于 2013-03-05T23:36:56.003 回答