0

I have json like this

{
"head": {
    "title": "Music",
    "status": "200"
},
"Info": [
    {
        "Name": "Mos Def",
        "Type": "music",
        "Results": [
            {
                "Name": "Talib Kweli",
                "Type": "music"
            },
            {
                "Name": "Black Star",
                "Type": "music"
            },
            {
                "Name": "Little Brother",
                "Type": "music"
            }
         ]
    }, 
    {
        "Name": "Mos Def",
        "Type": "Vehicles",
        "Results": [
            {
                "Name": "Chevy",
                "Type": "Car"
            },
            {
                "Name": "Ford",
                "Type": "Car"
            },
            {
                "Name": "Pontiac",
                "Type": "Car"
            }
         ]
      }
   ]
}

In my code I can get JSON Object Info to show type "Music, vehicles" in a list view . I wanna if I click Music it will show jsonObject Results - in other list view . How this solution?? thanks

1 Answer 1

0
On click of setOnItemSelectedListener of your listView set adapter for another listView.

I worked on similar scenarion in my case I was getting json response for departments and corresponding Team for departments. Onclick of departments I have to show team list for selected department.I have used spinners for displaying items .Here is the sample code for that :

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, mDeptmntList);
            dataAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mDeptmntSpinner.setAdapter(dataAdapter);
            mDeptmntSpinner
                    .setOnItemSelectedListener(new OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> arg0,
                                View arg1, int arg2, long arg3) {
                            int index = arg0.getSelectedItemPosition();
                            mSelectedDeptId = index;

                            ArrayAdapter<String> teamAdapter = new ArrayAdapter<String>(
                                    getApplicationContext(),
                                    android.R.layout.simple_spinner_item, mList
                                            .get(index));
                            teamAdapter
                                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            mTeamSpinner.setAdapter(teamAdapter);
                        }

                        public void onNothingSelected(AdapterView<?> arg0) {

                        }
                    });

this was my json response :

{
   "departmentId":"1",
   "departmentname":"HR",
   "teamlist":[
      {"teamName":"HR_S",
      "teamName":"HR_P"}
   ]
}


Code for fetching this json is :
        JSONArray array = new JSONArray(JSON response);
        JSONObject obj;
        mDeptmntList = new ArrayList<String>();
        mTeam = new ArrayList<ArrayList<Integer>>();
        for (int i = 0; i < array.length(); i++) {
            obj = (JSONObject) array.get(i);
            mDeptmntList.add(i, obj.getString("departmentname"));
            JSONArray teamList = obj.getJSONArray("teamlist");
            int count = teamList.length();
            for (int j = 0; j < count; j++) {
                mTeamTempId.add(j,teamList.getJSONObject(j).getString("teamName"));
            }
        }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.