2015-09-26 77 views
0

我想使用HashMap將值傳遞給其他類,但我不知道爲什麼我得到這樣奇怪的結果。我做了一個system.out.println,可重複使用[{Latitude = 1.224119,username = borrow,Longitude = 103.930041}],[{Latitude = 1.224119,username = borrow,Longitude = 103.930041}],但事實並非如此因爲我有兩個不同的座標。意圖不工作的散列圖

當我將它傳遞給另一個類時,它只返回一個座標而不是兩個。

我的代碼出了什麼問題?

得到分貝的座標,並傳遞給另一個類:

protected void onPostExecute(JSONObject json) { 
ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>(); 
HashMap<String, String> h1=new HashMap<String, String>(); 
      if (json != null) { 
       Toast.makeText(Borrower_AP.this, json.toString(), 
         Toast.LENGTH_LONG).show(); 

       try { 
        dataJsonArr = json.getJSONArray("Posts"); 
        for (int i = 0; i < dataJsonArr.length(); i++) { 
         JSONObject c = dataJsonArr.getJSONObject(i); 
         Longitude = c.getDouble("longitude"); 
         Latitude = c.getDouble("latitude"); 
         String s_lat = Latitude.toString(); 
         String s_long = Longitude.toString(); 
         h1.put("Latitude",s_lat); 
         h1.put("Longitude",s_long); 
         h1.put("username",username); 
         list.add(h1); 
Intent i = new Intent(this, BorrowerMap.class); 
       i.putExtra("list", list); 
       System.out.print(list); 
       startActivity(i); 
         } 

獲取值出:

ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>(); 
    list=(ArrayList<HashMap<String, String>>) getIntent().getExtras().get("list"); 
    String Latitude=list.get(0).get("Latitude"); 
    String Longitude=list.get(0).get("Longitude"); 
    String s_lat = Latitude.toString(); 
    String s_long = Longitude.toString(); 
    System.out.println(Latitude); 
    System.out.println(Longitude); 

回答

0

如果我按照你的問題不是讓意圖調用循環body.Because後只獲取一個列表項並在每個循環過程中進行意圖調用。

for (int i = 0; i < dataJsonArr.length(); i++) { 
         JSONObject c = dataJsonArr.getJSONObject(i); 
         Longitude = c.getDouble("longitude"); 
         Latitude = c.getDouble("latitude"); 
         String s_lat = Latitude.toString(); 
         String s_long = Longitude.toString(); 
         h1.put("Latitude",s_lat); 
         h1.put("Longitude",s_long); 
         h1.put("username",username); 
         list.add(h1); 
       } 
Intent i = new Intent(this, BorrowerMap.class); 
       i.putExtra("list", list); 
       System.out.print(list); 
       startActivity(i);