i want to open an image in another activity when clicked on the item in the listview

前端 未结 2 1655
耶瑟儿~
耶瑟儿~ 2021-01-23 14:44

I wrote the following code but I am sure that I did something wrong in the method onItemClick() My listview is displaying properly and when clicked upon another activity opens I

相关标签:
2条回答
  • 2021-01-23 15:09

    You should write onListItemClick() like this

    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));
        }
    }
    

    OUTPUT :

    enter image description here

    0 讨论(0)
  • 2021-01-23 15:14

    here you didnt define any location of your image. define the location string and put in the file and check it.

    String location="storage/img1.jpg";
    
    File imgfile = new File(location);
    
    0 讨论(0)
提交回复
热议问题