2014-06-12 33 views
1

我解析JSON數據到ListView中,併成功地解析在MainActivity.java,我在哪裏顯示主要地點的列表,如JSON的第一級:JSON削皮 - 如何顯示第二級別的ListView

  1. 內蒙古地點

  2. 外部位置

現在我想每當我做水龍頭的內部位置,然後在SecondActivity它應該顯示新德里的一個列表,這同樣適用於外祿ations還有,在這種情況下,每當用戶做水龍頭需要顯示美國

JSON的樣子:

{ 
    "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 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

注:但每當我做的第一個活動的任何列表項的挖掘,沒有得到SecondActivity中的任何列表,爲什麼?

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; 
    } 
+0

什麼是'strReceived'的價值? – Raghunandan

+0

@Raghunandan我得到正確的價值,就像我在內部位置點擊一樣然後得到:06-12 09:50:01.620:D /收到的數據::(4657):內部位置 – Sophie

+0

什麼是第73行? – Raghunandan

回答

2

試試下面的方法: -

 try 
     { 
      JSONObject j = new JSONObject("your json response"); 
      JSONArray all = j.getJSONArray("all"); 
      for (int i = 0; i < all.length(); i++) 
      { 
       JSONObject data = all.getJSONObject(i); 
       System.out.println("title  =>"+data.getString("title")); 
       JSONArray maps = data.getJSONArray("maps"); 
       for (int k = 0; k < maps.length(); k++) 
       { 
        JSONObject data_sec = maps.getJSONObject(k); 
        System.out.println("name  => "+data_sec.getString("name")); 
        System.out.println("latitude => "+data_sec.getString("latitude")); 
        System.out.println("longitude => "+data_sec.getString("longitude")); 

       } 


      } 
     } 
     catch (Exception e) 
     { 
      // TODO: handle exception 
     } 
0
// Try this way,hope this will help you to solve your problem. 

@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")); 
     map.put("index", i); 

     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)); 
     sendtosecond.putExtra("index", arraylist.get(position).get(MainActivity.INDEX)); 
     Log.d("Tapped Item::", arraylist.get(position).get(MainActivity.TITLE)); 
     Log.d("Tapped Item Index::", arraylist.get(position).get(MainActivity.INDEX)); 
     startActivity(sendtosecond); 
     } 
     }); 
     } 
     } 
     } 

@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"); 
     strReceivedIndex = in.getStringExtra("index"); 
     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("all").getJSONObject(Integer.parseInt(strReceivedIndex)).getgetJSONArray("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; 
    } 
+0

但我沒有在我的JSON中使用任何索引 – Sophie

+0

我知道,但你不能從標題獲得jsonArray,你必須得到它只使用該jsonObject的索引。 –

+0

嘗試你做什麼是首先從所有jsonArray獲得標題,並使用這個標題獲取地圖jsonArray在另一個屏幕是它還是別的? –