Home » Java » java – Making an image carousel using an Image Slider-Exceptionshub

java – Making an image carousel using an Image Slider-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am making an image carousel using Android Image Slider with Glide and I’m trying to test if I can implement ArrayList when adding URLs to the adapter but when I add the URLs to the ArrayList, the other data doesn’t show, only the last on the Array. It seems that I can’t store the URLs to the SliderItem’s value. Why is the other image not displaying on the Slider?

I used the default adapter of Android Image Slider: SliderAdapterExample.java.

Home.java

SliderItem sliderItem = new SliderItem();

    ArrayList<String> urls = new ArrayList<>();
    urls.add("https://lh3.googleusercontent.com/proxy/xdswuBCXXazoCcJ9t9c0y1DqcEU8Ixog4B6Q2MJaE3Iy84VV7g8GOS8C5b9JgTlFRGg23q_Rjw61Tr68A0mJ9ZnoqJdxPLLvCCZteQBOcaW61KEOQA");
    urls.add("https://www.luxuryproperty.com/uploads/images/6-bedroom-villa-for-sale-dubai_hills_mansions-LP0542-9ea75fa49452780.jpg");
    urls.add("https://www.luxuryproperty.com/uploads/images/7-bedroom-villa-for-sale-dubai_hills_mansions-LP0514-261aafdf24310c00.jpg");

    for(int i = 0; i < urls.size(); i++){
        sliderItem.setDescription("Slider Item Added Manually");
        sliderItem.setImageUrl(urls.get(i));
    }

    adapter.addItem(sliderItem);

SliderItem.java

public class SliderItem {
private String description;
private String imageUrl;

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}}

Here is what I got for now. Only the last link works. See image below:

Carousel

https://www.luxuryproperty.com/uploads/images/7-bedroom-villa-for-sale-dubai_hills_mansions-LP0514-261aafdf24310c00.jpg

How to&Answers:

You need to move adapter.addItem(sliderItem); within for loop.

    SliderItem sliderItem = new SliderItem();

    ArrayList<String> urls = new ArrayList<>();
    urls.add("https://lh3.googleusercontent.com/proxy/xdswuBCXXazoCcJ9t9c0y1DqcEU8Ixog4B6Q2MJaE3Iy84VV7g8GOS8C5b9JgTlFRGg23q_Rjw61Tr68A0mJ9ZnoqJdxPLLvCCZteQBOcaW61KEOQA");
    urls.add("https://www.luxuryproperty.com/uploads/images/6-bedroom-villa-for-sale-dubai_hills_mansions-LP0542-9ea75fa49452780.jpg");
    urls.add("https://www.luxuryproperty.com/uploads/images/7-bedroom-villa-for-sale-dubai_hills_mansions-LP0514-261aafdf24310c00.jpg");

    for(int i = 0; i < urls.size(); i++){
        sliderItem.setDescription("Slider Item Added Manually");
        sliderItem.setImageUrl(urls.get(i));

        // here
        adapter.addItem(sliderItem);
    }

or

    ArrayList<String> urls = new ArrayList<>();
    urls.add("https://lh3.googleusercontent.com/proxy/xdswuBCXXazoCcJ9t9c0y1DqcEU8Ixog4B6Q2MJaE3Iy84VV7g8GOS8C5b9JgTlFRGg23q_Rjw61Tr68A0mJ9ZnoqJdxPLLvCCZteQBOcaW61KEOQA");
    urls.add("https://www.luxuryproperty.com/uploads/images/6-bedroom-villa-for-sale-dubai_hills_mansions-LP0542-9ea75fa49452780.jpg");
    urls.add("https://www.luxuryproperty.com/uploads/images/7-bedroom-villa-for-sale-dubai_hills_mansions-LP0514-261aafdf24310c00.jpg");

    for(int i = 0; i < urls.size(); i++){
        SliderItem sliderItem = new SliderItem();
        sliderItem.setDescription("Slider Item Added Manually");
        sliderItem.setImageUrl(urls.get(i));

        // here
        adapter.addItem(sliderItem);
    }

According to your code, you have three url, so you need three SliderItem.But if you put adapter.addItem(sliderItem); outside for loop, you can get last SliderItem only.

Additionally, everytime you add an item to adapter, you need to set adapter.notifyDataSetChanged() to addItem().

If my solution has any problem, please let me free to know.