Search code examples
androidnetwork-programmingandroid-recyclerviewflickr

Same image in imageview of recyclerview networking


I am developing an Android App.It uses recyclerview which has an ImageView and the image view is populated from Flickr by Networking Code. The problem is all of my imageview shows the same image.What i am doing wrong?Please help


Solution

  • The problem seems to be here:

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

    You don't initialize a new photo through each iteration of your jsonArray loop (i.e. you're just setting new values for the same photo object and adding a copy of that photo each time)

    You should edit this function to look like this:

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