Android expandableListView 错误

Android expandableListView error

我有一个 expandableListView 正在解析 JSON 数据。但问题是每次我得到相同的组名,并且每个组都具有相同的 childs。

我的 JSON 数据是

[
    {
        "PrvCusID": "10000000",
        "CusCategory": null,
        "Title": null,
        "CusName": "Rakib Hossain",
        "Address": null,
        "City": null,
        "Phone": null,
        "Email": null,
        "DOB": "0001-01-01T00:00:00",
        "Active": null,
        "ClubName": null,
        "CardNo": "123",
        "PinNo": null,
        "BalanceAmount": 0,
        "Picture": null,
        "tempSales": [
            {
                "TempID": 2014,
                "CounterId": "",
                "UserId": "raj",
                "PrvCusID": "10000000",
                "Item_id": "26",
                "OptionsID": "",
                "AddonsID": "",
                "OptionsGroupName": "",
                "ItemName": "popcorn",
                "AddonsName": "",
                "OptionsName": "",
                "RPU": 75,
                "VAT": 0,
                "QTY": 1,
                "CPU": 0,
                "GuestNo": 0,
                "TblNo": "Pool table 1",
                "IsHappyHourPrice": "N",
                "IsPrinted": "N",
                "ShopID": "S01",
                "CustomerName": "Rakib Hossain",
                "ItemSL": 1,
                "OrderNo": "S012015070100001",
                "Area": "null",
                "RPU_Text": "75",
                "QTY_Text": "1",
                "Instruction": "",
                "InstructionIDs": "",
                "IsInvoicePrint": null,
                "CardNo": null
            },
            {
                "TempID": 2013,
                "CounterId": "",
                "UserId": "raj",
                "PrvCusID": "10000000",
                "Item_id": "27",
                "OptionsID": "",
                "AddonsID": "",
                "OptionsGroupName": "",
                "ItemName": "chips",
                "AddonsName": "",
                "OptionsName": "",
                "RPU": 50,
                "VAT": 0,
                "QTY": 1,
                "CPU": 0,
                "GuestNo": 0,
                "TblNo": "Pool table 1",
                "IsHappyHourPrice": "N",
                "IsPrinted": "N",
                "ShopID": "S01",
                "CustomerName": "Rakib Hossain",
                "ItemSL": 1,
                "OrderNo": "S012015070100001",
                "Area": "null",
                "RPU_Text": "50",
                "QTY_Text": "1",
                "Instruction": "",
                "InstructionIDs": "",
                "IsInvoicePrint": null,
                "CardNo": null
            }
        ]
    },
    {
        "PrvCusID": "10000001",
        "CusCategory": null,
        "Title": null,
        "CusName": "Sehav",
        "Address": null,
        "City": null,
        "Phone": null,
        "Email": null,
        "DOB": "0001-01-01T00:00:00",
        "Active": null,
        "ClubName": null,
        "CardNo": "456",
        "PinNo": null,
        "BalanceAmount": 0,
        "Picture": null,
        "tempSales": [
            {
                "TempID": 2016,
                "CounterId": "",
                "UserId": "raj",
                "PrvCusID": "10000001",
                "Item_id": "23",
                "OptionsID": "",
                "AddonsID": "",
                "OptionsGroupName": "",
                "ItemName": "patat met",
                "AddonsName": "",
                "OptionsName": "",
                "RPU": 175,
                "VAT": 0,
                "QTY": 1,
                "CPU": 0,
                "GuestNo": 0,
                "TblNo": "Pool table 1",
                "IsHappyHourPrice": "N",
                "IsPrinted": "N",
                "ShopID": "S01",
                "CustomerName": "Sehav",
                "ItemSL": 1,
                "OrderNo": "S012015070100001",
                "Area": "null",
                "RPU_Text": "175",
                "QTY_Text": "1",
                "Instruction": "",
                "InstructionIDs": "",
                "IsInvoicePrint": null,
                "CardNo": null
            },
            {
                "TempID": 2015,
                "CounterId": "",
                "UserId": "raj",
                "PrvCusID": "10000001",
                "Item_id": "28",
                "OptionsID": "",
                "AddonsID": "",
                "OptionsGroupName": "",
                "ItemName": "peanuts",
                "AddonsName": "",
                "OptionsName": "",
                "RPU": 125,
                "VAT": 0,
                "QTY": 1,
                "CPU": 0,
                "GuestNo": 0,
                "TblNo": "Pool table 1",
                "IsHappyHourPrice": "N",
                "IsPrinted": "N",
                "ShopID": "S01",
                "CustomerName": "Sehav",
                "ItemSL": 1,
                "OrderNo": "S012015070100001",
                "Area": "null",
                "RPU_Text": "125",
                "QTY_Text": "1",
                "Instruction": "",
                "InstructionIDs": "",
                "IsInvoicePrint": null,
                "CardNo": null
            }
        ]
    }
]

这里CusName是组名,数组"tempSales"中的元素是child 我的代码如下

   StringRequest stringRequest=new StringRequest(Method.GET, baseUrl+"/dc/Api/Sales/GetOrderPreviewByCustomers?orderNo="+orderno, 
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    ArrayList<Group> group_list = new ArrayList<Group>();
                    ArrayList<Child> ch_list;
                    try {
                          Group gru = new Group();

                          ch_list = new ArrayList<Child>();
                        jsonArray = new JSONArray(response);

                        for (int i = 0; i < jsonArray.length(); i++) {

                            String customerName = jsonArray.getJSONObject(i)
                                    .getString("CusName");


                            JSONArray childArray=jsonArray.getJSONObject(i).getJSONArray("tempSales");
                            Child ch = new Child();
                            for (int j = 0; j < childArray.length(); j++) {



                                String itemName=childArray.getJSONObject(j).getString("ItemName");
                                String itemQuantity=childArray.getJSONObject(j).getString("QTY_Text");
                                String unitPrice=childArray.getJSONObject(j).getString("RPU_Text");

                                ch.setName(itemName);
                                ch.setQuantity(itemQuantity);
                                ch.setUnitPrice(unitPrice);

                                ch_list.add(ch);

                            }

                            gru.setName(customerName);
                            gru.setItems(ch_list);

                            group_list.add(gru);                                
                        }


                          ExpAdapter = new ExpandListAdapter(
                                    getApplicationContext(), group_list);

                          ExpandList.setAdapter(ExpAdapter);

                            PD.dismiss();

                    } catch (JSONException e) {

                    }


                }

    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {
                     PD.dismiss();

            }
            });
    AppController.getInstance().addToRequestQueue(stringRequest);

我该如何解决这个问题?提前致谢。

移动

Child ch = new Child();

在循环中。

for (int j = 0; j < childArray.length(); j++) {

您多次将同一个对象添加到列表中

在 childArray 循环中创建子对象的新对象

StringRequest stringRequest=new StringRequest(Method.GET, baseUrl+"/dc/Api/Sales/GetOrderPreviewByCustomers?orderNo="+orderno, 
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    ArrayList<Group> group_list = new ArrayList<Group>();
                    ArrayList<Child> ch_list;
                    try {



                        jsonArray = new JSONArray(response);

                        for (int i = 0; i < jsonArray.length(); i++) {
                                      Group gru = new Group();

                                  ch_list = new ArrayList<Child>();

                            String customerName = jsonArray.getJSONObject(i)
                                    .getString("CusName");


                            JSONArray childArray=jsonArray.getJSONObject(i).getJSONArray("tempSales");

                            for (int j = 0; j < childArray.length(); j++) {
                                Child ch = new Child();



                                String itemName=childArray.getJSONObject(j).getString("ItemName");
                                String itemQuantity=childArray.getJSONObject(j).getString("QTY_Text");
                                String unitPrice=childArray.getJSONObject(j).getString("RPU_Text");

                                ch.setName(itemName);
                                ch.setQuantity(itemQuantity);
                                ch.setUnitPrice(unitPrice);

                                ch_list.add(ch);

                            }

                            gru.setName(customerName);
                            gru.setItems(ch_list);

                            group_list.add(gru);                                
                        }


                          ExpAdapter = new ExpandListAdapter(
                                    getApplicationContext(), group_list);

                          ExpandList.setAdapter(ExpAdapter);

                            PD.dismiss();

                    } catch (JSONException e) {

                    }


                }

    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {
                     PD.dismiss();

            }
            });
 StringRequest stringRequest=new StringRequest(Method.GET, baseUrl+"/dc/Api/Sales/GetOrderPreviewByCustomers?orderNo="+orderno, 
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    ArrayList<Group> group_list = new ArrayList<Group>();
                    ArrayList<Child> ch_list;
                    try {
                         // Group gru = new Group();

                          ch_list = new ArrayList<Child>();
                        jsonArray = new JSONArray(response);

                        for (int i = 0; i < jsonArray.length(); i++) {

                           // create in inside the loop
                               Group gru = new Group();

                            String customerName = jsonArray.getJSONObject(i)
                                    .getString("CusName");


                            JSONArray childArray=jsonArray.getJSONObject(i).getJSONArray("tempSales");
                          //  Child ch = new Child();
                            for (int j = 0; j < childArray.length(); j++) {
                                  // create in inside the loop 
                                  Child ch = new Child()

                                String itemName=childArray.getJSONObject(j).getString("ItemName");
                                String itemQuantity=childArray.getJSONObject(j).getString("QTY_Text");
                                String unitPrice=childArray.getJSONObject(j).getString("RPU_Text");

                                ch.setName(itemName);
                                ch.setQuantity(itemQuantity);
                                ch.setUnitPrice(unitPrice);

                                ch_list.add(ch);

                            }

                            gru.setName(customerName);
                            gru.setItems(ch_list);

                            group_list.add(gru);                                
                        }


                          ExpAdapter = new ExpandListAdapter(
                                    getApplicationContext(), group_list);

                          ExpandList.setAdapter(ExpAdapter);

                            PD.dismiss();

                    } catch (JSONException e) {

                    }


                }

    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {
                     PD.dismiss();

            }
            });
    AppController.getInstance().addToRequestQueue(stringRequest);