1
我正在開發一個Android應用程序。它使用recyclerview有一個ImageView 和圖像視圖由Flickr由網絡代碼填充。 問題是我所有的ImageView的顯示了相同的image.What我做錯了,請幫imageview的recyclerview聯網相同的圖像
我正在開發一個Android應用程序。它使用recyclerview有一個ImageView 和圖像視圖由Flickr由網絡代碼填充。 問題是我所有的ImageView的顯示了相同的image.What我做錯了,請幫imageview的recyclerview聯網相同的圖像
這個問題似乎在這裏:
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;
}