0
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        convertView = inflater.inflate(resource, null);
    }

    ImageView imageView;
    imageView = (ImageView) convertView.findViewById(R.id.ivGallery);
    for(HospitalModel.Images images: hospitalModelList.get(position).getImagesList()) {
        Glide.with(getContext()).load(images).into(imageView);
    }
    return convertView;
}

// image URLs are stored in string ArrayList . I defined getter and setter for array list but still I don't know how to use get method for showing ArrayList images dynamically in ListView

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Safiullah
  • 39
  • 6
  • Needs more code. It looks like your `HospitalModelList` contains objects that contain an `ArrayList` object. If that's so, then you need some sort of logic to determine WHICH image of that list needs to be picked and loaded. – Chantell Osejo Dec 22 '16 at 19:45
  • yes , you are saying right . in this ArrayList , i have images urls. i want to show these images in single imageView of listview dynamically. – Safiullah Dec 22 '16 at 19:49

2 Answers2

0

Extend the class from BaseAdapter

Override getCount() method and return here the total amount of images you need to show.

In the getView load with Glide only ONE url (remove the for loop), the getView method will be called N times to show in the list the "total" amount of images you returned in the getCount method, the position parameter in the getView method will run from 0 to (total - 1).

You should map 1 position to 1 url, maybe you will need to change the way you access the objects that contain the urls.

Tassadar
  • 224
  • 3
  • 7
  • Can you share any source code of fetching images using urls arraylist in base adapter of listview / gridview / recyclerView ? I want to create images gallery. Thanks a lot – Safiullah Dec 23 '16 at 00:19
0

Okay so first: stop using ListView, use RecyclerView.

Next, you need to override getItemCount(). You can either choose to do this using a for loop:

int count = 0;

for(HospitalModel.Images images: hospitalModelList.get(position).getImagesList()) {
    count += images.size();
}

Or, what is likely preferable, flatten your model object into just what this adapter actually cares about (which is the image URL strings). Something like this:

ArrayList<String> imageUrls = new ArrayList<String>();

for(HospitalModel model : hospitalModelList) {
    imageUrls.addAll(model.getImagesList().getImageUrls());
}

Then pass in the imageUrls ArrayList to the Adapter instead. You should only need to flatten this when the model is updated (whether that's initialization or changed). When that occurs, use the notify... methods on the adapter.

After that, it's just a simple Glide.with(getContext()).load(imageUrls.get(position)).into(imageView); inside getView (or onBindViewHolder if you're using a RecyclerView, which I HIGHLY recommend).

Chantell Osejo
  • 1,456
  • 15
  • 25
  • Can you share any source code of fetching images using urls arraylist in base adapter of listview / gridview / recyclerView ? I want to create images gallery. Thanks a lot – Safiullah Dec 23 '16 at 00:19
  • [Here's an example tutorial](http://www.androidhive.info/2016/04/android-glide-image-library-building-image-gallery-app/) FYI, worth doing a simple Google search in the future for such requests. That link was the top result for "recyclerview image gallery" search. That code should at least get you started. It isn't optimal but will do the trick. – Chantell Osejo Dec 23 '16 at 21:09