从JSONObject到ListView的Android数据显示格式错误 [英] Android data display in wrong format from JSONObject to ListView

查看:119
本文介绍了从JSONObject到ListView的Android数据显示格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用HashMapListview显示2列数据,它工作正常.当我尝试显示多于2列时出现错误.

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"
}

上面的编码工作正常.但是,当我尝试在多列中显示数据时,就会出现错误,

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"]
}

从上面的屏幕截图中可以看到,我希望Key为482,483,484 ...,其值在右侧.编码有什么问题?

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?

推荐答案

似乎JSON中的值是JSONArray,因此response.getString(key)会将整个数组返回为String.

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

调用getJSONArray()以获取JSONArray的值,因此您可以单独处理这些项目.

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();
}

这篇关于从JSONObject到ListView的Android数据显示格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆