Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JSON parsing in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 1.56k
    Answer it

    I have problem with parsing JSON data in android. I want to populate the following json data to spinners.
    I have populated the categories to 1st spinner but i need to populate the 2nd spinner for subcategories which is dependent on 1st spinner. When user select the category from 1st spinner then 2nd spinner should display the 
    only the first subarray(subcategories)  i.e "sub category 1" and "sub category 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"
                        }
                    ]
                }
            ]

     

     

    Below is the java code to populate the spinners, but i have problems with conditions. Please solve my problem

     

    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);
                                myArrList.add(map);
    
                                //Log.i("Rsp", "onResponse: " + categoryId + "------Title: " + categorytitle);
                            }
    
                            SimpleAdapter simpleAdapter;
                            simpleAdapter = new SimpleAdapter(SelectProduct.this, 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);
                                }
    
                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {
    
                                }
                            });
    
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
    
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
    
                    }
                });
    
        Volley.newRequestQueue(this).add(request);
    }
    
    
    
    
    public void populateSpinnerSubCategories(final int categoryId) {
    
        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);
    
                                    //Toast.makeText(SelectProduct.this, "ID: " + categoryId, Toast.LENGTH_SHORT).show();
    
                                    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(SelectProduct.this, 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(SelectProduct.this, "Connection problem!", Toast.LENGTH_SHORT).show();
                    }
                });
    
        Volley.newRequestQueue(this).add(request);
    }

     

     

    JSON parsing in android

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: