gosulove gosulove - 2 months ago 16
Android Question

Android data display in wrong format from JSONObject to ListView

I try to display 2 column data with

HashMap
and
Listview
, it works fine. The error comes when I try to display more than 2 columns.

2_columns.java

public class Main4Activity extends AppCompatActivity {

String url = "http://192.168.1.103/web_service/omg.php/";
private static final String COLUMN_ID = "ID";
private static final String COLUMN_NAME = "Name";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

try {
Iterator<String> iterator = response.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String value = response.getString(key);

HashMap<String, String> map = new HashMap<>();
map.put(COLUMN_ID, key);
map.put(COLUMN_NAME, value);

aList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}

if(aList.size() > 0) {
String[] from = {COLUMN_ID, COLUMN_NAME};
int[] to = {R.id.text_id, R.id.text_name};
SimpleAdapter adapter = new SimpleAdapter(Main4Activity.this, aList, R.layout.list_item, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", "error ");
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsObjRequest);
}
}


localhost/web_service/omg.php

{
"32":"Western Food",
"33":"Chinese Food",
"34":"Mix Food",
"35":"Japanese Food",
"36":"Korean Food",
"37":"Italian Food",
"38":"German Food"
}


enter image description here

The above coding is working fine. But the errors come when I try to display data in multiple columns,

multiple_columns.java

String url = "http://192.168.1.103/web_service/ohno.php/";

private static final String product_sku = "SKU";
private static final String product_name = "Name";
private static final String product_price = "Price";
private static final String product_quantity = "Quantity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

try {
Iterator<String> iterator = response.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String value_sku = response.getString(key);
String value_name= response.getString(key);
String value_price= response.getString(key);
String value_quantity= response.getString(key);

HashMap<String, String> map = new HashMap<>();
map.put(product_sku, value_sku);
map.put(product_name, value_name);
map.put(product_price, value_price);
map.put(product_quantity, value_quantity);

aList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}

if(aList.size() > 0) {
String[] from = {product_sku,product_name, product_price,product_quantity};
int[] to = {R.id.sku,R.id.name, R.id.price,R.id.quantity};
SimpleAdapter adapter = new SimpleAdapter(Main5Activity.this, aList, R.layout.list_item2, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", "error ");
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsObjRequest);
}


localhost/web_service/ohno.php

{
"482":["1","Chicken Rice","1","1"],
"483":["1","French Fries","1","1"],
"484":["1","apple","1","1"],
"492":["1","western+italian","1","1"],
"493":["1","no_cat","1","1"]
}


enter image description here

As you can see from the above screenshot, I want the Key to be 482,483,484... and the value its on the right side. What's wrong with the coding?

Answer

Seems like your values are JSONArrays, so response.getString(key) will return the whole array as a String.

Call getJSONArray() to get the values as a JSONArray, so you can process the items individually.

Something like this should work:

try {
    Iterator<String> iterator = response.keys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        JSONArray array = response.getJSONArray(key);
        HashMap<String, String> map = new HashMap<>();

        map.put(product_sku, array.getString(0));
        map.put(product_name, array.getString(1));
        map.put(product_price, array.getString(2));
        map.put(product_quantity, array.getString(3));

        aList.add(map);
    }
} catch (JSONException e) {
    e.printStackTrace();
}