Recyclerview 网络的图像视图中的相同图像

Same image in imageview of recyclerview networking

我正在开发 Android App.It 使用具有 ImageView 的 recyclerview 并且图像视图是通过网络代码从 Flickr 填充的。 问题是我所有的图像视图都显示相同的 image.What 我做错了吗?请帮忙

问题似乎出在这里:

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