Brendom
Brendom

Reputation: 123

get image from a listview onlistitemclick

I can get into my custom directory by this code which displays me the list of images in my directory now what i want is to open those image when i click on a image list .

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

I have hard-code my imgfile as my image name which is displaying the image . I want to dynamically change the image on my selection

Upvotes: 0

Views: 474

Answers (1)

Gabriella Angelova
Gabriella Angelova

Reputation: 2985

the position parameter in the protected void onListItemClick( ListView l, View v, int position, long id) {...} function shows the position of the element in your listView which is selected, so you could get the name of your selected image by String name =(String) l.getItemAtPosition(position); or you can do another thing without using the position parameter and it's to use String name = ((TextView) view).getText(); And then you could easily use the name variable in your line File imgFile = new File(root+name);

Upvotes: 1

Related Questions