代码之家  ›  专栏  ›  技术社区  ›  user5434198

recyclerview网络的imageview中的相同图像

  •  1
  • user5434198  · 技术社区  · 7 年前

    我正在开发一个Android应用程序。它使用具有ImageView的recyclerview 图像视图由网络代码从Flickr填充。

    1 回复  |  直到 7 年前
        1
  •  1
  •   anomeric    7 年前

    问题似乎在这里:

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

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

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