2017-07-27 40 views

回答

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; 
} 
相關問題