Parsing data from Json as a collection of image to Picasso

前端 未结 2 667
攒了一身酷
攒了一身酷 2021-01-29 09:51

I tried to parse a collection of image to recyclerview as gridview using Picasso library but i cannot make it work

Note : i dont h

2条回答
  •  时光说笑
    2021-01-29 10:17

    You would need a List imageUrls to save urls to be viewed by picasso, no need for special object for that. To solve it, change your json parsing code to

    try {
        JSONArray entries = response.getJSONObject("feed").getJSONArray("entry");
        int count = entries.length();
        for (int i = 0; i < count; i++) {
            JSONObject imageJson = entries.getJSONObject(i).getJSONObject("im:image");
            // in case you want to get image with height 53
            String imageUrl = imageJson.getJSONObject("0").getString("label");
    
            // String imageUrl = imageJson.getJSONObject("1").getString("label"); height 75
            // String imageUrl = imageJson.getJSONObject("2").getString("label"); height 100
    
            // add urls to the list
            imageUrls.add(imageUrl);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    

    then change your adapter to have a List imageUrls instead of List imagesModule and pass the parsed list to the constructor. Finally in your adapter onBindViewHolder() change the picasso code to

    Picasso.with(context).load(imageUrls.get(position)).into(holder.appImage);
    

    hope it helps

提交回复
热议问题