JSON Paring - How to show second Level ListView

Posted by Sophie on Stack Overflow See other posts from Stack Overflow or by Sophie
Published on 2014-06-12T04:58:42Z Indexed on 2014/06/13 9:26 UTC
Read the original article Hit count: 239

Filed under:
|
|

I am parsing JSON data into ListView, and successfully parsed first level of JSON in MainActivity.java, where i am showing list of Main Locations, like:

  1. Inner Locations

  2. Outer Locations

Now i want whenever i do tap on Inner Locations then in SecondActivity it should show Delhi and NCR in a List, same goes for Outer Locations as well, in this case whenever user do tap need to show USA

JSON look like:

{
    "all": [
        {
            "title": "Inner Locations",
            "maps": [
                {
                    "title": "Delhi",
                    "markers": [
                        {
                            "name": "Connaught Place",
                            "latitude": 28.632777800000000000,
                            "longitude": 77.219722199999980000 
                        },
                        {
                            "name": "Lajpat Nagar",
                            "latitude": 28.565617900000000000,
                            "longitude": 77.243389100000060000
                        }
                    ]
                },
                {
                    "title": "NCR",
                    "markers": [
                        {
                            "name": "Gurgaon",
                            "latitude": 28.440658300000000000,
                            "longitude": 76.987347699999990000
                        },
                        {
                            "name": "Noida",
                            "latitude": 28.570000000000000000,
                            "longitude": 77.319999999999940000
                        }
                    ]
                }
            ]
        },
        {
            "title": "Outer Locations",
            "maps": [
                {
                    "title": "United States",
                    "markers": [
                        {
                            "name": "Virgin Islands",
                            "latitude": 18.335765000000000000,
                            "longitude": -64.896335000000020000
                        },
                        {
                            "name": "Vegas",
                            "latitude": 36.114646000000000000,
                            "longitude": -115.172816000000010000
                        }
                    ]
                }
            ]
        }
    ]
}

Note: But whenever i do tap on any of the ListItem in first activity, not getting any list in SecondActivity, why ?

MainActivity.java:-

@Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://10.0.2.2/locations.json");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("all");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrieve JSON Objects
                    map.put("title", jsonobject.getString("title"));

                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();

            listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_LONG).show();
                    // TODO Auto-generated method stub                  
                    Intent sendtosecond = new Intent(MainActivity.this, SecondActivity.class);
                    // Pass all data rank
                    sendtosecond.putExtra("title", arraylist.get(position).get(MainActivity.TITLE));
                    Log.d("Tapped Item::", arraylist.get(position).get(MainActivity.TITLE));
                    startActivity(sendtosecond);
                }
            });         
        }
    }
}

SecondActivity.java:

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);

    Intent in = getIntent();
    strReceived = in.getStringExtra("title");
    Log.d("Received Data::", strReceived);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
}

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://10.0.2.2/locations.json");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("maps");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrieve JSON Objects
                map.put("title", jsonobject.getString("title"));

                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about android