0

I'm trying to show images array in a list but I didn't figured it out yet, can anyone please help me. This may seem as a repeated question, but I tried other answers, they didn't work.

My JSON:

[
  {
    "id": 52,
    "name": "viber",
    "images": [
      "7829-hit-the-vibe.gif",
      "8413_Lit.gif",
      "8095_Lazergunzoncam.gif",
      "7090_LaserCamz.gif",
      "2123-vibe-4.gif",
      "3175-vibe-3.gif",
      "7413-vibe-2.gif",
      "1100-vibe.gif",
      "6381-lazeroncamz-2.gif"
    ],
    "download": null,
    "amount": 9
  },
{
    "id": 45,
    "name": "aesthetic",
    "description": "a",
    "slug": "6039-aesthetic",
    "images": [
      "4071_planet.png",
      "3499_love_it.png",
      "3019_space_bottle.png",
      "6033_pixel_flower.png",
      "1620-cupcake-pink.gif",
      "2760-seashell-pink.gif",
      "1794_sparkles.gif",
      "2523_RamSip.png"
    ],
    "download": null,
    "amount": 8
  },
]

My code:

String jsonString = "myjson";

json1 = new Gson().fromJson(jsonString, new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());

for(int _repeat24 = 0; _repeat24 < (int)(json1.size()); _repeat24++) {

    JSONObject obj = new JSONObject(jsonString);
    JSONArray getArray = obj.getJSONArray("images");
    JSONObject objects = getArray.getJSONObject(_repeat24);
    Iterator key = objects.keys();
    while (key.hasNext()) {
            String value = key.next();
    }
    
}

My task: I'm trying to save the array images as json in a key value, example:

categoriesMap = new HashMap<>();
categoriesMap.put("name", name);
categoriesMap.put("imagesJson", JSON_I_NEED);
json1.add(categoriesMap);

This should work as a loop for all the array positions. Thanks.

2
  • Please add the code you have so far for accessing the rest of the json Commented Mar 20, 2021 at 10:30
  • can you send your full code? it's not enough and I don't know how did you access to that json and please say that json storied in which variable Commented Mar 20, 2021 at 10:40

2 Answers 2

1

String response = "YOUR JSON ARRAY FROM API RESPONSE";

try {
    JSONArray jsonArray = new JSONArray(response);

    List<List<String>> imagesList = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {

        JSONObject obj = jsonArray.getJSONObject(i);
        JSONArray imagesArray = obj.getJSONArray("images");
        List<String> images = new ArrayList<>();

        for (int j = 0; j < imagesArray.length(); j++) {
            images.add(imagesArray.getString(j));
        }
        imagesList.add(images);
    }
    // Now you have got a list of a list of images
    Log.e(TAG, "onCreate: " + imagesList.toString());

} catch (JSONException e) {
    e.printStackTrace();
    Log.e(TAG, "onCreate: ", e);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use this way to parse your response :

ArrayList<Data> dataList = new ArrayList<Data>();
if(response!=null){
    JSONArray mainArray = new JSONArray(response);
    for(int i =0; i< mainArray.length();i++){
       JSONObject itemObject = mainArray.optJSONObject(i);
       Data data = new Data();
       data.setName(itemObject.optString("name"));
       ArrayList<String> images = new ArrayList<String>();
       // parsing images array 
        String images = itemObject.optJSONArray("images").toString();

        // this will set images array for each position : ["a.jpg","b.jpg","c.jpg"]
        data.setImages(images);
        dataList.add(data);
    }
    
}

Now populate your list adapter with this ArrayList you will be able to access images for each position distinctly

To store images for each position :

Create a Data Class like this :

class Data {
  int id ; 
  String name; 
  String images;
  int amount; 


 public String getName() {
    return name;
  }

public void setName(String name) {
    this.name = name;
}

public String getImages(){
  return images;
}

public void setImages(String images){
  this.images = images;
}

}

Now to access the images in recycler view adapter :

dataList.get(position).getImages(); // ["a.jpg","b.jpg"]

6 Comments

I updated my json code to understand what i want, can you please check it. Every position must include its own array values, not all arrays of all positions.
How do you need to show the data in the recycler view if it is position wise you can get it like this for position 0 : if dataList is ArrayList<Data> then dataList.get(0).getImages() and you will get the images for that position . OR you need to show all the images of all positions combined ? I can't understand your exact requirement .
Your first statement is exactly what I want. I don't want to show them all combined.
And do you need only the image array for each position or you need other data too like name , id , with it ? I will update my answer accordingly .
I need the image array for each position and also the other keys like name ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.