2017-09-26 65 views
0

我想從來自Web服務的json數據中填充spinners。我有一個名爲「categoriesList」的主數組,其中包含名爲「子類別」的子數組,但這些數組具有相同的名稱幷包含數據對象。我想根據1st spinner的選擇即categoriesList填充第二個子類的微調。問題是,當我從第一個微調(categoriesList)中選擇一個項目,然後第二個微調顯示所有的子陣列,但我只需要相應的子陣列顯示在第二個微調(子類別)。如何解析來自android中的web服務的主數組中具有相同名稱的json子數組?

以下是JSON數據格式:

{ 
"success": true, 
"code": 200, 
"message": "Data Found.", 
"content": { 
    "companyTypes": [ 
     { 
      "id": 1, 
      "title": "Distributor" 
     }, 
     { 
      "id": 2, 
      "title": "Mechanics" 
     }, 
     { 
      "id": 3, 
      "title": "Retailer" 
     }, 
     { 
      "id": 4, 
      "title": "Others" 
     } 
    ], 
    "citiesList": [ 
     { 
      "id": 1, 
      "name": "Sharja" 
     } 
    ], 
    "brandsList": [ 
     { 
      "id": 1, 
      "title": "Test Brand" 
     }, 
     { 
      "id": 2, 
      "title": "Brand 2" 
     }, 
     { 
      "id": 3, 
      "title": "Brand 3" 
     }, 
     { 
      "id": 4, 
      "title": "Brand 4" 
     }, 
     { 
      "id": 5, 
      "title": "Brand 5" 
     } 
    ], 
    "categoriesList": [ 
     { 
      "id": 1, 
      "title": "Test Category", 
      "subCategories": [ 
       { 
        "id": 1, 
        "title": "sub category 1" 
       }, 
       { 
        "id": 5, 
        "title": "Sub Category 5" 
       } 
      ] 
     }, 
     { 
      "id": 2, 
      "title": "Category 2", 
      "subCategories": [ 
       { 
        "id": 2, 
        "title": "Sub Category 2" 
       }, 
       { 
        "id": 3, 
        "title": "Sub Category 3" 
       } 
      ] 
     }, 
     { 
      "id": 3, 
      "title": "Category 3", 
      "subCategories": [ 
       { 
        "id": 4, 
        "title": "Sub Category 4" 
       } 
      ] 
     } 
    ] 
} 
} 

這在我的Android Java代碼:

所屬分類代碼

public void populateSpinnerCategories() { 
    StringRequest request = new StringRequest(Request.Method.GET, urlConfigurationsCategories, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        try { 
         final ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>(); 
         HashMap<String, String> map; 

         jsonObjectResponse = new JSONObject(response); 
         jsonObjectContent = jsonObjectResponse.getJSONObject("content"); 

         jsonArrayCompanyTypes = jsonObjectContent.getJSONArray("categoriesList"); 

         for (int i = 0; i < jsonArrayCompanyTypes.length(); i++) { 
          jsonObjectID = jsonArrayCompanyTypes.getJSONObject(i); 

          distributorId = jsonObjectID.getInt("id"); 

          title = jsonObjectID.getString("title"); 

          map = new HashMap<String, String>(); 
          map.put("title", title); 
          map.put("id", distributorId + ""); 
          myArrList.add(map); 

          //Log.i("Rsp", "onResponse: " + distributorId + "------Title: " + title); 
         } 

         SimpleAdapter simpleAdapter; 
         simpleAdapter = new SimpleAdapter(getActivity(), myArrList, R.layout.activity_show, 
           new String[]{"title"}, 
           new int[]{R.id.tv_column_name}); 
         spinnercategoriesList.setAdapter(simpleAdapter); 

         spinnercategoriesList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
          @Override 
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
           selectedCompanyType = myArrList.get(position).get("title").toString(); 

           categoryId = (int) (spinnercategoriesList.getSelectedItemId() + 1); 

           //populateCompanies(DIST_ID); 

           //TODO populate the subcategories spinner 
           populateSpinnerSubCategories(categoryId); 

           loadCategories(); 
          } 

          @Override 
          public void onNothingSelected(AdapterView<?> parent) { 

          } 
         }); 

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

       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 

    Volley.newRequestQueue(getActivity()).add(request); 
} 

子類別代碼

public void populateSpinnerSubCategories(final int categoryId) { 

    String url = "http://radial-energy.com/radial/api/configurations"; 

    StringRequest request = new StringRequest(Request.Method.GET, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         final ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>(); 
         HashMap<String, String> map; 

         String subCategory = null; 
         int subCatId = 0; 

         jsonObjectResponse = new JSONObject(response); 
         jsonObjectContent = jsonObjectResponse.getJSONObject("content"); 

         JSONArray jsonArrayCategories = jsonObjectContent.getJSONArray("categoriesList"); 

         for (int i = 0; i < jsonArrayCategories.length(); i++) { 
          jsonObjectID = jsonArrayCategories.getJSONObject(i); 

          JSONArray jsonArraySubCategories = new 
            JSONArray(jsonArrayCategories.getJSONObject(i) 
            .getString("subCategories")); 

          System.out.println("Sub Categories Array: " + jsonArraySubCategories); 

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

           JSONObject jsonObjectSubCategories = jsonArraySubCategories.getJSONObject(j); 

           subCatId = jsonObjectSubCategories.getInt("id"); 

           subCategory = jsonObjectSubCategories.getString("title"); 

           Log.d("CATEGORY: ", subCategory); 

           map = new HashMap<String, String>(); 
           map.put("title", subCategory); 
           map.put("id", subCatId + ""); 
           myArrList.add(map); 
          } 
         } 

         SimpleAdapter simpleAdapter; 
         simpleAdapter = new SimpleAdapter(getActivity(), myArrList, 
           R.layout.activity_show, new String[]{"title"}, 
           new int[]{R.id.tv_column_name}); 

         spinnerSubcategoriesList.setAdapter(simpleAdapter); 

         spinnerSubcategoriesList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
          @Override 
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
           String selectedSubCategory = myArrList.get(position).get("title").toString(); 

           //DIST_ID = (int) (spinnerSubcategoriesList.getSelectedItemId() + 1); 

           //populateCompanies(DIST_ID); 
          } 

          @Override 
          public void onNothingSelected(AdapterView<?> parent) { 

          } 
         }); 

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

      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(getActivity(), error.getMessage().toString(), Toast.LENGTH_SHORT).show(); 
       } 
      }); 

    Volley.newRequestQueue(getActivity()).add(request); 
} 
+2

嘗試使用GSON,它會讓你的生活更輕鬆。 –

回答

0

描述數據模型

class DataItem { 
    String id; 
    String title; 
    List<DataItem> subCategories; 
} 

class DataContent { 
    List<DataItem> companyTypes; 
    List<DataItem> citiesList; 
    List<DataItem> brandsList; 
    List<DataItem> categoriesList; 
} 

class DataResponse { 
    Boolean success; 
    Integer code; 
    String message; 
    DataContent content; 
} 

,只需使用GSON解析你的迴應

@Override 
public void onResponse(String response) { 
    Gson gson = new GsonBuilder().create(); 
    DataResponse dataResponse = gson.fromJson(response, DataResponse.class); 
    ... 
+0

謝謝您的迴應!主席先生,我已經做了大部分事情。 spinners是填充的,但我很困惑如何使用if條件當用戶從第一個微調項目中選擇一個項目,並用其各自的數組填充另一個微調項目。項目的ID已經作爲參數傳遞給方法。問題出在第二種方法的第二個for循環中。請解決我的問題。我被困在這裏。 –

相關問題