Reputation: 106
I am using the AndroidImageSlider library in my app for an image slider. I am loading images using a Hashmap
like this:
HashMap<String,String> url_maps = new HashMap<String, String>();
url_maps.put("Image 1", "http://example.com/./project_image/example.jpg");
url_maps.put("Image 2", "http://example.com/./project_image/example.jpg");
url_maps.put("Image 3", "http://example.com/./project_image/example.jpg");
for(String name : url_maps.keySet()){
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView
.description(name)
.image(url_maps.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit);
//add your extra information
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",name);
mDemoSlider.addSlider(textSliderView);
}
Note: I am setting the key of the HashMap
as the description for each image.
How do I make this slider dynamic?
I want to load the images dynamically from a json feed which looks like this:
{
"Images": [
{
"title": "title",
"Image": "http://example.com/./project_image/example.jpg"
},
{
"title": "Distribution",
"Image": "http://example.com/./project_image/example.jpg"
},
{
"title": "Distribution",
"Image": "http://example.com/./project_image/example.jpg"
}
]
}
Can I do it using a HashMap
or do I need to use an alternative? If yes please suggest. Or do I use a different library? If yes please give reference.
Thank you.
Upvotes: 0
Views: 1876
Reputation: 1739
use Gson
Gson gson = new Gson();
Type type= new TypeToken<List<ImageData>>(){}.getType();
List<ImageData> imageDataList = gson.fromJson(jsonString, type);
where jsonString
is the string representing your JSON array
the ImageData
class would look like this
public class ImageData {
String title;
String Image;
public ImageData(){};
}
then iterate over the list as follows
for(ImageData data : imageDataList){
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView
.description(data.title)
.image(data.Image)
.setScaleType(BaseSliderView.ScaleType.Fit);
//add your extra information
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",data.title);
mDemoSlider.addSlider(textSliderView);
}
Upvotes: 1
Reputation: 2954
Create a class as a Structure of your Data
class ImageUrls{
String imageTitle,imageUrl;
ImageUrls(String imageTitle, String imageUrl){
this.imageTitle = imageTitle;
this.imageUrl = imageUrl;
}
}
Now make a ArrayList<ImageUrls> url_maps
Add data in the ArrayList
url_maps.add("Image 1", "http://example.com/./project_image/example.jpg");
url_maps.add("Image 2", "http://example.com/./project_image/example.jpg");
url_maps.add("Image 3", "http://example.com/./project_image/example.jpg");
and add this data to your ImageSlider
For(int count : ArrayList.size()){
//here you can get your data from arraylist using
arraylist.get(postion).imageTitle
arraylist.get(postion).imageUrl
}
Upvotes: 0