2014-06-12 3 views
1

내가 ListView에로 JSON 데이터를 분석하고, 성공적으로 내가 좋아, 홈페이지 위치의 목록을 표시하고 MainActivity.java에서 JSON의 첫 번째 수준을 분석하고 있습니다 :JSON 껍질 벗기기 - 표시하는 방법 두 번째 수준의 ListView

  1. 내부 위치

지금 내가 다음 SecondActivity에서이 목록에 델리를 표시해야합니다 내부 위치에 탭 않을 때마다 원하는

  • 외부 위치, 같은 외부 Loc 데이에 간다 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 
             } 
            ] 
           } 
          ] 
         } 
        ] 
    } 
    

    참고 :하지만 난 처음 활동에서을 ListItem의에 탭 않을 때마다, 점점하지 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/Received Data :(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

    나는 그것을 알지만 당신은 그 jsonObject의 인덱스를 사용하여 얻을 수있는 제목에서 jsonArray를 얻을 수 없습니다. –

    +0

    당신이하려고하는 것은 먼저 모든 jsonArray에서 제목을 얻고이 제목을 사용하여 다른 화면에서 jsonArray map을 얻는 것입니까 아니면 다른 것입니까? –

    관련 문제