Integer[] leftItems = new Integer[]{R.drawable.ic_1, R.drawable.ic_2, R.drawable.ic_3, R.drawable.ic_4, R.drawable.ic_5};
Integer[] rightItems = new Integer[]{R.drawable.ic_6, R.drawable.ic_7, R.drawable.ic_8, R.drawable.ic_9, R.drawable.ic_10};
leftAdapter = new ItemsAdapter(this, R.layout.item, leftItems);
rightAdapter = new ItemsAdapter(this, R.layout.item, rightItems);
listViewLeft.setAdapter(leftAdapter);
listViewRight.setAdapter(rightAdapter);
leftViewsHeights = new int[leftItems.length];
rightViewsHeights = new int[rightItems.length];
对于左侧列表视图onclick我使用下面的代码
listViewLeft.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Integer name = (Integer) arg0.getItemAtPosition(arg2);
Intent Courses = new Intent(ItemsActivity.this, ViewMyImage.class);
Courses.putExtra("position",arg2);
Courses.putExtra("name",name.toString());
startActivity(Courses);
}
});
在ViewMyImage.java中,我使用此代码获取值
int value = getIntent().getExtras().getInt("position");
String name = getIntent().getExtras().getString("name");
Toast.makeText(getApplicationContext(), name,Toast.LENGTH_LONG).show();
我想要图像的值ic_X,我可以再次显示图像。
答案 0 :(得分:1)
使用getResources().getIdentifier
尝试从字符串名称中找到drawable:
int value = getIntent().getExtras().getInt("position");
int drawableid = getResources().getIdentifier("ic_"+value ,
"drawable", getPackageName());
Drawable drawableimg = getResources().getDrawable(drawableid);
现在使用drawableimg
使用ImageView.setImageDrawable
并且发送方式是传递Drawable id,而不是使用ViewMyImage
从上一个Activity到Courses.putExtra("position",leftItems[arg2])
Activity的位置。在第二个Activity中,您可以直接将值传递给setImageDrawable
id