在recyclerview网络的imageview中的相同图像

时间:2017-07-27 16:05:25

标签: android networking android-recyclerview flickr

我正在开发一个Android App。它使用了带有ImageView的recyclerview 并且图像视图由Flickr通过网络代码填充。 问题是我的所有imageview都显示了相同的图像。我做错了什么?请帮忙

1 个答案:

答案 0 :(得分:1)

问题似乎在这里:

public List<Photo> downloadGalleyItem(String url){
    photoList=new ArrayList<>();
    Photo photo=new Photo();
    String jsonString=getData(url);
    try {
        JSONObject jsonObject=new JSONObject(jsonString);
        JSONArray jsonArray=jsonObject.getJSONArray("items");

        for(int i=0;i<jsonArray.length();i++){
            JSONObject jsonObject1=jsonArray.getJSONObject(i);
            photo.setTitle(jsonObject1.getString("title"));
            photo.setAuthor(jsonObject1.getString("author"));
            photo.setAuthorId(jsonObject1.getString("author_id"));
            photo.setTag(jsonObject1.getString("tags"));

            JSONObject jsonMedia =jsonObject1.getJSONObject("media");
            String imageUrl=jsonMedia.getString("m");
            photo.setImage(jsonMedia.getString("m"));

            //we are changing _m to _b so that when image is tapped we get biigger image
            photo.setLink(imageUrl.replaceAll("_m.","_b."));
            photoList.add(photo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return photoList;
}

您不会通过jsonArray循环的每次迭代初始化新照片(即,您只需为同一照片对象设置新值并每次都添加该照片的副本)

您应该编辑此函数,如下所示:

public List<Photo> downloadGalleyItem(String url){
    photoList=new ArrayList<>();
    Photo photo=null;
    String jsonString=getData(url);
    try {
        JSONObject jsonObject=new JSONObject(jsonString);
        JSONArray jsonArray=jsonObject.getJSONArray("items");

        for(int i=0;i<jsonArray.length();i++){
            JSONObject jsonObject1=jsonArray.getJSONObject(i);
            photo = new Photo();
            photo.setTitle(jsonObject1.getString("title"));
            photo.setAuthor(jsonObject1.getString("author"));
            photo.setAuthorId(jsonObject1.getString("author_id"));
            photo.setTag(jsonObject1.getString("tags"));

            JSONObject jsonMedia =jsonObject1.getJSONObject("media");
            String imageUrl=jsonMedia.getString("m");
            photo.setImage(jsonMedia.getString("m"));

            //we are changing _m to _b so that when image is tapped we get biigger image
            photo.setLink(imageUrl.replaceAll("_m.","_b."));
            photoList.add(photo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return photoList;
}