0

enter image description here

My CustomList AdapterCode...

private final Activity context;

private List<String> list;

public CustomListAdapter(Activity context, List<String> list) {
    super(context, R.layout.list_single);

    this.context = context;
    this.list = list;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list_single, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);


    if (list != null) {
        for (int i = 0; i < list.size(); i++) {

            File imgFile = new File(list.get(i));

            if (imgFile.exists()) {

                ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                imageView.setImageBitmap(myBitmap);
            }
        }
    }

    return rowView;
}

@Override
public int getCount() {
    return list.size();
}

}

I am receiving a list of imagePath like

d: Array path/storage/sdcard/DCIM/Camera/IMG_20170210_163055.jpg

d: Array path/storage/sdcard/DCIM/Camera/IMG_20170210_163037.jpg

d: Array path/storage/sdcard/DCIM/Camera/IMG_20170217_175804.jpg

d: Array path/storage/sdcard/DCIM/Camera/IMG_20170217_175802.jpg

d: Array path/storage/sdcard/DCIM/Camera/IMG_20170210_163056.jpg

Now I want to show these paths to list view either using a custom adapter or using ArrayAdapter.

Oriol Roma
  • 329
  • 1
  • 5
  • 9
Kuldeep Singh
  • 950
  • 1
  • 9
  • 17

1 Answers1

1

Let's say you are sending an ArrayList of paths tp your custom adapter. Now in the getView() method of custom adapter, add the following:

File imgFile = new  File(myList.get(position));

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) convertView.findViewById(R.id.imageviewTest);
    //the imageview of your custom list item

    myImage.setImageBitmap(myBitmap);

}

Let me know if you need help with custom adapter

NOTE: If you have a lot of images, I would suggest you to use library like Picasso or Glide

Do no use a for loop inside getview. GetView is itself called everytime a row is created.

View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);


if (list != null) {

        File imgFile = new File(list.get(position));

        if (imgFile.exists()) {

            ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            imageView.setImageBitmap(myBitmap);
        }
}
return rowView;
Nirup Iyer
  • 1,215
  • 2
  • 14
  • 20
  • It works for me. at least now I am able to see the image but I am getting the last image continuously in listView. – Kuldeep Singh Apr 17 '17 at 07:50
  • @Niprup Thanks Now Its Perfect, i was using look to populate that list of collection to list view that was wrong logic can u pls specify why that was wrong concept. – Kuldeep Singh Apr 17 '17 at 08:14
  • Every adapter has a getView() method. When the listview needs to populate rows, it calls the getView method. getView method, in your case, is inflating list_single layout into a view, setting textview and imageview and returning the inflated view. Now listview will insert this inflated view as a row. What you were doing was, you were looping inside getView, hence for every row, for loop is iterated and the last path in the list is set as image. – Nirup Iyer Apr 17 '17 at 08:20
  • @NipurIyer need one more help in my case how can I select a single image from listview or from Grid view(In First Tab Same Images are there) and Display that Image to Another Fragment. – Kuldeep Singh Apr 17 '17 at 08:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141862/discussion-between-nirup-iyer-and-kuldeep-singh). – Nirup Iyer Apr 17 '17 at 08:33