我目前有一个网格视图,它可以将图像位置/ID 返回给父活动。我还想将图像 Uri 返回或提取到父活动,并将其设置为可以在父活动的其他地方解析的变量。我一直在寻找一点,但找不到适合我正在尝试的解决方案。
我返回位置的gridview子活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent data = new Intent(PicPicker2.this, PictureActivity.class);
// passing array index
data.putExtra("id", position);
setResult(Activity.RESULT_OK, data);
finish();
}
});
}
}
我目前在父活动中捕获结果的地方
else if (reqCode == 2) {
// Selected image id
int position = data.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
}
我想在哪里解析变量
Uri attached_Uri = Uri.parse("SOME VARIABLE OF THE RETURNED URI");
我想将 Uri attach_Uri = Uri.parse() 设置为在 gridview 中单击的项目。我只是一个周末的 Android 修补匠,试图学习平台的概念,所以解释和示例对我来说有很长的路要走,非常感谢。
添加了我的图像适配器
公共类 ImageAdapter 扩展 BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}