2016-06-29 54 views
0

我在我的应用程序中使用AndroidImageSlider库作为图像滑块。我加载使用Hashmap像这样的图片:动态图像滑块Android

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

注:我设置HashMap的为每个图像描述的关键。

如何使此滑块动态?

我想从一个JSON提要看起来像这样动态加载图片:它采用了一块HashMap

{ 
    "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" 
    } 
    ] 
} 

能做些什么或做我需要使用一个替代?如果是,请提出建议。或者我使用不同的图书馆?如果是,请给予参考。 谢谢。

回答

0

创建一个类来作为你的数据

class ImageUrls{ 
    String imageTitle,imageUrl; 
    ImageUrls(String imageTitle, String imageUrl){ 
     this.imageTitle = imageTitle; 
     this.imageUrl = imageUrl; 
    } 
} 

现在结构做出ArrayList<ImageUrls> url_maps

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

添加数据,并将这些数据添加到您的ImageSlider

For(int count : ArrayList.size()){ 
    //here you can get your data from arraylist using 
    arraylist.get(postion).imageTitle 
    arraylist.get(postion).imageUrl 

    } 
+0

我不能使用哈希映射来做这个吗? –

+0

这种方法在制作ImageSlider时适用于我,我认为它也可以使用哈希映射,但从未尝试过。 –

1

使用Gson

Gson gson = new Gson(); 
Type type= new TypeToken<List<ImageData>>(){}.getType(); 
List<ImageData> imageDataList = gson.fromJson(jsonString, type); 

其中jsonString是代表你的JSON数组的字符串

ImageData类是这样的

public class ImageData { 
    String title; 
    String Image; 
    public ImageData(){}; 
} 

然后在列表上迭代如下

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