0
        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];

对于 leftlist 视图 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 的值,我可以再次显示图像。

4

1 回答 1

1

尝试使用getResources().getIdentifier从字符串名称中查找可绘制对象:

int value = getIntent().getExtras().getInt("position");
int drawableid = getResources().getIdentifier("ic_"+value , 
                                                 "drawable", getPackageName());
Drawable drawableimg = getResources().getDrawable(drawableid);

现在drawableimg用于设置 ImageView 背景图像使用ImageView.setImageDrawable

并且发送方式是传递 Drawable id,而不是如果位置从以前的 Activity 到ViewMyImageActivity 使用Courses.putExtra("position",leftItems[arg2]). 在第二个活动中,您可以setImageDrawable直接将值传递给 id

于 2013-03-29T13:48:35.187 回答