Search code examples
androiddata-structuresarray-map

Output form ArrayMap equals to null


I'm writing a parser to print a file json on my mobile. I include an ArrayMap to save data received from the file. My problem is that when I have to print data in a TextView, it returns null. I included the code under my question.

ArrayMap<Integer, String> Array = new ArrayMap<>();
for(int i=0; i < arr.length(); i++){
    JSONObject jsonPart = arr.getJSONObject(i);
    Array.put("Main", jsonPart.getString("main"));
    Array.put("Description", jsonPart.getString("description"));
    Array.put("Id", jsonPart.getString("id"));
    TxT.append(Txt.getText() + Array.get(i));
}

Solution

  • First of all you should not use the key word Array as it is preserve word, you can change it to something like jsonMap. You should use ArrayMap<String, String> if you inserting String in the key element. So your code will look like this:

     ArrayMap<String, String> jsonMap = new ArrayMap<>();
     for(int i=0; i<arr.length(); i++){
          JSONObject jsonPart = arr.getJSONObject(i);
          jsonMap.put("Main", jsonPart.getString("main"));
          jsonMap.put("Description", jsonPart.getString("description"));
          jsonMap.put("Id", jsonPart.getString("id"));
          TxT.append(TxT.getText() + jsonMap.get(i));
    

    }