大家中午后好! 我在expandable listview中面临一个问题>我没有在expandable listview中得到准确的正确结果。 我在每个ListGroupElement中重复使用child。 我得到这种格式的结果:
这是我的json格式:
{
"status": 1,
"category": [
{
"id": 1,
"category_name_en": "car",
"category_name_ar": "car",
"description_en": "",
"description_ar": "",
"day": "monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"status": "A",
"created_at": "Sep 4 2017 5:39 PM",
"updated_at": "Sep 4 2017 5:39 PM",
"sub_category": [
{
"id": 1,
"sub_category_name_en": "**Audi**",
"sub_category_name_ar": "Audi",
"sub_description_en": "",
"sub_description_ar": "",
"status": "A",
"created_at": "Sep 4 2017 5:40 PM",
"updated_at": "Sep 4 2017 5:40 PM"
},
{
"id": 4,
"sub_category_name_en": "**New**",
"sub_category_name_ar": "New",
"sub_description_en": "",
"sub_description_ar": "",
"status": "A",
"created_at": "Sep 4 2017 7:15 PM",
"updated_at": "Sep 4 2017 7:15 PM"
}
]
},
{
"id": 2,
"category_name_en": "Animal",
"category_name_ar": "Animal",
"description_en": "",
"description_ar": "",
"day": "monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"status": "A",
"created_at": "Sep 4 2017 6:49 PM",
"updated_at": "Sep 4 2017 6:49 PM",
"sub_category": [
{
"id": 2,
"sub_category_name_en": "**Dogs**",
"sub_category_name_ar": "Dogs",
"sub_description_en": "",
"sub_description_ar": "",
"status": "A",
"created_at": "Sep 4 2017 6:50 PM",
"updated_at": "Sep 4 2017 6:50 PM"
},
{
"id": 3,
"sub_category_name_en": "**Cat**",
"sub_category_name_ar": "Cat",
"sub_description_en": "",
"sub_description_ar": "",
"status": "A",
"created_at": "Sep 4 2017 6:51 PM",
"updated_at": "Sep 4 2017 6:51 PM"
}
]
}
]
我想要这种格式的结果
这是我的解析
private ArrayList<ModelGroup> modelgroup = null;
private ArrayList<ModelChild> modelchild = null;
private ArrayList<ArrayList<ModelChild>> ListChildXXXXXXXXX= null;
ModelGroup LT = null;
ModelChild CT = null;
String response = result.getResult().toString();
JSONObject jsonRoot = new JSONObject(response);
JSONArray jsonArray = jsonRoot.getJSONArray("category");
for (int i = 0; i < jsonArray.length(); i++) {
LT= new ModelGroup();
JSONObject jsonObject = jsonArray.getJSONObject(i);
LT.setCat_name(jsonObject.getString("category_name_en"));
// LT.setCat_id(jsonObject.getString("id"));
modelgroup.add(LT);
JSONArray jsonArray1 = jsonObject.getJSONArray("sub_category");
for (int j = 0; j < jsonArray1.length(); j++) {
CT = new ModelChild();
JSONObject jsonObject2 = null;
jsonObject2 = jsonArray1.getJSONObject(j);
CT.setSub_category_name_en(jsonObject2.getString("sub_category_name_en"));
}
//get(i).get(i).add(CT);
modelchild.add(CT);
ListChildXXXXXXXXX.add(modelchild);
}
请纠正我在哪里做错了, 谢谢你的帮助。