我不能流利地说英语,而且我是新手。请耐心等待。
问题:
我想将图像从第一个活动传递到第二个活动
但是在putExtra中出现了问题。
什么是问题?请帮帮我。
代码:
public class First extends Activity implements OnClickListener, OnItemClickListener {
ArrayList<Integer> arrayItem;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
arrayItem = new ArrayList<Integer>();//
arrayItem.add(R.drawable.top1);
arrayItem.add(R.drawable.top2);
arrayItem.add(R.drawable.top3);
arrayItem.add(R.drawable.top4);
First_Adapter adapter = new First_Adapter(this, arrayItem);
GridView grid = (GridView) this.findViewById(R.id.gridView1);
grid.setAdapter(adapter);
final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String imgName="iv1";
Intent in =new Intent(First.this, Second.class);
in.putExtra("imgName", iv1);
startActivity(in);
finish();
}
});
// grid.setOnClickListener(this);
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
iv1.setImageResource(arrayItem.get(arg2));
}
});
}
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent in = getIntent();
String imgName=in.getStringExtra("imgName");
int iv1 = getResources().getIdentifier(imgName, "drawable", getPackageName());
Bitmap bm =((BitmapDrawable) getBaseContext().getResources().getDrawable(iv1)).getBitmap();
ImageView iv =(ImageView) findViewById(R.id.imageView2);
iv.setImageBitmap(bm);
}
答案 0 :(得分:1)
改变你的行
String imgName="iv1";
Intent in =new Intent(First.this, Second.class);
in.putExtra("imgName", imgName);
startActivity(in);