从列表视图 onlistitemclick 获取图像

get image from a listview onlistitemclick

我可以通过此代码进入我的自定义目录,该代码显示我的目录中的图像列表,现在我想要的是在我单击图像列表时打开这些图像。

private void getDir(String dirPath) {
    myPath.setText("Location: " + dirPath);
    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(dirPath);
    File[] files = f.listFiles();
    if (!dirPath.equals(root)) {
        item.add(root);
        path.add(root);
        item.add("../");
        path.add(f.getParent());
    }

    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        path.add(file.getPath());
        if (file.isDirectory())
            item.add(file.getName() + "/");
        else
            item.add(file.getName());
    }
    ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.explorernew, item);
    setListAdapter(fileList);
}

File file;

@Override
protected void onListItemClick( ListView l, View v, int position, long id) {
    File imgFile = new  File(root+"/20150424_173923.jpg");
    //  TextView stxt = (TextView) findViewById(R.id.textView2);
    // stxt.setText(List.get(position));

    if (imgFile.exists()) {
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ImageView myImage = (ImageView) findViewById(R.id.imageView);
        myImage.setImageBitmap(myBitmap);
    }
}

我已将我的 imgfile 硬编码为显示图像的图像名称。我想动态更改我选择的图像

protected void onListItemClick( ListView l, View v, int position, long id) {...} 函数中的 position 参数显示被选中的元素在 listView 中的位置,因此您可以通过 String name =(String) l.getItemAtPosition(position); 或你可以在不使用 position 参数的情况下做另一件事,它是使用 String name = ((TextView) view).getText(); 然后您可以轻松地在 File imgFile = new File(root+name);

行中使用 name 变量