JSON转换为不添加到Listview的字符串

时间:2017-06-02 04:12:26

标签: android json listview

我正在从我的服务器检索数据并且它在JSON ARRAY中,当它第一次检索时它在JSON的listview中显示,但是当我将它转换为字符串并再次添加到我的listview时它没有显示,但是当我在TOAST中展示它时,它可以工作。

我可能做错了什么?

这是我的活动:

   private void showLIST(JSONArray jsonArray) {
    JSONObject obj = null;

    //RETRIEVES ALL THE DATA FROM THE SERVER
    for (int i = 0; i < jsonArray.length(); i++) {
        try {
            obj = jsonArray.getJSONObject(i);
            orderID.add(obj.getString(ORDERID));
            orderUserNumber.add(obj.getString(ORDERUSERNUMBER));
            orderName.add(obj.getString(ORDERNAME));
            orderDateTime.add(obj.getString(ORDERDATETIME));

        } catch (JSONException e) {
            e.printStackTrace();
        }

    //THIS CONVERTS THE JSON TO STRING AND DISPLAYS IT IN A TOAST 
        try {
            name = obj.getString(ORDERNAME);
            JSONArray jsonarray = new JSONArray(name);
            for (int x = 0; x < jsonarray.length(); x++) {
                JSONObject jsonobject = jsonarray.getJSONObject(x);
                names = jsonobject.getString("name");

             //   orderName.add(obj.getString(names));
                Toast.makeText(viewOrdersPage.this, names, Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    viewOrderAdapter listOrderAdapter = new viewOrderAdapter(this,orderID, orderUserNumber, orderName, orderDateTime);
    listView.setAdapter(listOrderAdapter);
}

我的适配器代码:

  private Context context;

private ArrayList<String> orderID;
private ArrayList<String> orderUserNumber;
private ArrayList<String> orderName;
private ArrayList<String> orderDateTime;

public viewOrderAdapter(Context context, ArrayList<String> orderID, ArrayList<String> orderUserNumber, ArrayList<String> orderName, ArrayList<String> orderDateTime){
    this.context = context;
    this.orderID = orderID;
    this.orderUserNumber = orderUserNumber;
    this.orderName = orderName;
    this.orderDateTime = orderDateTime;
}

@Override
public int getCount() {
    return orderID.size();
}

@Override
public Object getItem(int position) {
    return orderID.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final View listView;
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listView = inflater.inflate(R.layout.view_orders_item, null);

    TextView id = (TextView) listView.findViewById(R.id.orderID);
    TextView number = (TextView) listView.findViewById(R.id.orderUserNumber);
    TextView name = (TextView) listView.findViewById(R.id.orderName);
    TextView dateTime = (TextView) listView.findViewById(R.id.orderDateTime);


    id.setText(orderID.get(position));
    number.setText(orderUserNumber.get(position));
    name.setText(orderName.get(position));
    dateTime.setText(orderDateTime.get(position));

    return listView;
}

这是我的JSON响应:

[{"orderID":"PQ6B1MlsSMnl6IrxlN7pgkHEQock6G"
,"userNumber":"+639161693630"
,"orderName":"[{\"name\":\"RED HORSE\",\"size\":\"500ML\",\"quantity\":\"4 
BOTTLE\"}]","orderDateTime":"02\/06\/2017, 04:28 am"}

这是我的logcat,它没有在Listview中显示已转换的JSON:

   W/System.err: org.json.JSONException: No value for RED HORSE
W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
W/System.err:     at com.example.gin.orderingcompiledv1.viewOrdersPage.showLIST(viewOrdersPage.java:208)
W/System.err:     at com.example.gin.orderingcompiledv1.viewOrdersPage.access$300(viewOrdersPage.java:46)
W/System.err:     at com.example.gin.orderingcompiledv1.viewOrdersPage$2.onResponse(viewOrdersPage.java:136)
W/System.err:     at com.example.gin.orderingcompiledv1.viewOrdersPage$2.onResponse(viewOrdersPage.java:133)
W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:145)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6918)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

提前感谢您的帮助! :d

0 个答案:

没有答案