我想在列表视图中单击项目时在另一个活动中打开图像

时间:2014-10-29 11:54:41

标签: android listview

我编写了以下代码,但我确信我在onItemClick()方法中做错了 我的列表视图正常显示,当点击另一个活动打开时,我想只显示新活动中的图像 所以需要你们的帮助

MainActivity

public class MainActivity extends ActionBarActivity {

String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
        "Saturday", "Sunday" };
int[] images = { R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher };

int[] images1 = { R.drawable.image01, R.drawable.image02,
        R.drawable.image03, R.drawable.image04, R.drawable.image05,
        R.drawable.image06, R.drawable.image07 };
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.listView1);

    ArjAdapter adapter = new ArjAdapter(getBaseContext(), days, images);

    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {

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

            File imgfile = new File("");
            if (imgfile.exists()) {
                String myBitmap = imgfile.getAbsolutePath();
                Intent intent = new Intent(MainActivity.this,
                        ImageActivity.class);
                intent.setType("image/*");
                intent.putExtra("img", "images1");
                startActivity(intent);
            }
        }
    });

在上面的方法中,我不确定要在文件对象中包含什么,这个方法我看到了某个地方,但我不确定要包含哪些内容,所以留空以向你们咨询

ImageActivity,我想在其中显示图像

public class ImageActivity extends Activity {
ImageView iv;

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

    Bitmap myBitmap = BitmapFactory.decodeFile(getIntent().getStringExtra(
            "img"));

    ImageView iv = (ImageView) findViewById(R.id.imageView2);
    iv.setImageBitmap(myBitmap);
}

欢迎任何建议

2 个答案:

答案 0 :(得分:1)

你应该像这样写onListItemClick()

lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Intent intent = new Intent(MainActivity.this,
                        ImageActivity.class);
                intent.putExtra("img", images1[position]);
                startActivity(intent);


            }
        });

<强> ImageActivity.java

public class ImageActivity extends Activity {
    ImageView iv;

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


        ImageView iv = (ImageView) findViewById(R.id.imageView2);
        iv.setImageResource(getIntent().getIntExtra("img", 0));
    }
}

输出:

enter image description here

答案 1 :(得分:0)

这里你没有定义你的图像的任何位置。定义位置字符串并放入文件并进行检查。

String location="storage/img1.jpg";

File imgfile = new File(location);