gosulove gosulove - 2 months ago 18
JSON Question

Android JsonArray failed to retrieve all the values and display in Listview with SimpleAdapter

I try to display data that I parse from Json into Listview with SimpleAdapter.

I can display all the data except there is a problem with multiple values in a JsonArray.

enter image description here

Json data

{
"57": {
"Steak Doness": [
"well done",
"medium",
"rare"
]
},
"58": {
"Size": [
"small",
"medium",
"large",
"extra large"
]
},
"59": {
"Cooking Method": [
"spicy",
"non-spicy"
]
},
"63": {
"Coldness": [
"with ice",
"without ice"
]
}
}


java coding

JSONObject jsonObject = new JSONObject(response);
String get_option_data=jsonObject.getString("option_data");
JSONObject jsonObject1 = new JSONObject(get_option_data);
Iterator<String> iterator = jsonObject1.keys();
while (iterator.hasNext()) {

String oid = iterator.next();
String name_value=jsonObject1.getString(oid);
JSONObject jsonObject2=new JSONObject(name_value);
Iterator<String> iterator1 = jsonObject2.keys();

while (iterator1.hasNext()) {
String name=iterator1.next();
HashMap<String, String> map = new HashMap<>();
map.put("oid",oid);
map.put("name",name);
JSONArray jsonArray=jsonObject2.getJSONArray(name);
int i;
for(i=0;i<jsonArray.length();i++){
map.put("value",jsonArray.getString(i));
}
aList.add(map);
}
}
String[] from = {"oid","name","value"};
int[] to = {R.id.oid,R.id.name,R.id.value};
SimpleAdapter adapter = new SimpleAdapter(option.this, aList, R.layout.option_template, from, to);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);


As you can see, it only display the last element of JsonArray but not every element in that JsonArray. Anyone knows what's wrong?

Jai Jai
Answer

Here you are overriding multiple values on the same position of hashmap. So you have to create either new hashmap for multiple values or pass that all value comma seperated into single key

Your mistake :

while (iterator1.hasNext()) {
        String name=iterator1.next();
        HashMap<String, String> map = new HashMap<>();
        map.put("oid",oid);
        map.put("name",name);
        JSONArray jsonArray=jsonObject2.getJSONArray(name);
        int i;
        for(i=0;i<jsonArray.length();i++){
            map.put("value",jsonArray.getString(i));
        }
        aList.add(map);
    }

Solution:

while (iterator1.hasNext()) {
        String name=iterator1.next();
        HashMap<String, String> map = new HashMap<>();
        map.put("oid",oid);
        map.put("name",name);
        JSONArray jsonArray=jsonObject2.getJSONArray(name);
        int i;
        String myValues = "";
        for(i=0;i<jsonArray.length();i++){
            if(myValues.equalsIgnoreCase(""))
               myValues = jsonArray.getString(i);
            else
               myValues = myValues +","+jsonArray.getString(i);
        }
        map.put("value",myValues);
        aList.add(map);
    }

In your adpater, You will prase this string and will create new array

String[] values = "yourcommaseperatedValue".spilt(",");

And display this values array in Textview Like;

for(int i = 0 ; i < values.length(); i++){
   yourtextview.setText(values[i]+"\n");
}

Better Approach

You could take ArrayList to store multiple values but in that case you will have to take new hashmap with type

HashMap<String, ArrayList<String>> map = new HashMap<>();

Where you will store you unique id "oid" as key and value would be your values jsonArray.

Comments