2017-09-05 68 views
2

我做的,從URL解析獲取數據爲水平列表視圖動態數據 其工作正常,但由JSON對象的只顯示最後一個值。我已經粘貼下面我的JSON解析和json.Please建議我在哪裏做錯了。如何獲得在水平列表視圖

這是我的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 8:09 AM", 
      "updated_at": "Sep 4 2017 8:09 AM", 
      "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 8:10 AM", 
        "updated_at": "Sep 4 2017 8:10 AM" 
       }, 
       { 
        "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 9:45 AM", 
        "updated_at": "Sep 4 2017 9:45 AM" 
       } 
      ] 
     }, 
     { 
      "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 9:19 AM", 
      "updated_at": "Sep 4 2017 9:19 AM", 
      "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 9:20 AM", 
        "updated_at": "Sep 4 2017 9:20 AM" 
       }, 
       { 
        "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 9:21 AM", 
        "updated_at": "Sep 4 2017 9:21 AM" 
       } 
      ] 
     } 
    ] 
} 

這是我的解析

在這裏,我做我的分析,它的返回JSON對象大小正確的,但只顯示最後一個值

我錯了。請給我建議

public static ArrayList<CardData> PaeseBanner(String response) throws JSONException { 
    ArrayList<CardData> alUser = new ArrayList<>(); 

    JSONObject jsonRoot = new JSONObject(response); 

    JSONArray jsonArray = jsonRoot.getJSONArray("category"); 

    CardData LT = new CardData(); 

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

     JSONObject jsonObject = jsonArray.getJSONObject(i); 

     JSONObject jsonObject2 = null; 

     JSONArray jsonArray1 = jsonObject.getJSONArray("sub_category"); 

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

      jsonObject2 = jsonArray1.getJSONObject(j); 

      LT.setSubcategory_name(jsonObject2.getString("sub_category_name_en")); 

      alUser.add(LT); 
     } 

    } 

    return alUser; 
} 

這是我的屏幕短 This is Screen Short

回答

0

試試這個

for (int j = 0; j < jsonArray1.length(); j++) { 
    CardData LT = new CardData(); // Create new CardData object inside second for loop 

    jsonObject2 = jsonArray1.getJSONObject(j); 

    LT.setSubcategory_name(jsonObject2.getString("sub_category_name_en")); 

    alUser.add(LT); 
} 
0

使用這些行:

CardData LT = new CardData(); 

JSONObject jsonObject2 = null; 

內部for循環。像這樣:

public static ArrayList<CardData> PaeseBanner(String response) throws JSONException { 
    ArrayList<CardData> alUser = new ArrayList<>(); 

    JSONObject jsonRoot = new JSONObject(response); 

    JSONArray jsonArray = jsonRoot.getJSONArray("category"); 



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

     JSONObject jsonObject = jsonArray.getJSONObject(i); 

     JSONArray jsonArray1 = jsonObject.getJSONArray("sub_category"); 

     for (int j = 0; j < jsonArray1.length(); j++) { 
      JSONObject jsonObject2 = null; 

      jsonObject2 = jsonArray1.getJSONObject(j); 

      LT.setSubcategory_name(jsonObject2.getString("sub_category_name_en")); 

      alUser.add(LT); 
     } 

    } 

    return alUser; 
}