2013-11-26 95 views
0

我想做一個for循環來從HashMap獲取值。我試過這個:for循環從HashMap獲取值

final ArrayList<String> list = new ArrayList<String>(); 
      for (int i = 0; i < places1.size(); ++i) { 

       list.addAll(places1.values()); 

      } 

但它只帶來第一個元素並打印很多次,我該怎麼做?

其中places1是HashMap中,我從另一個活動了:

HashMap<String, String> places1=(HashMap<String, String>) extras.getSerializable("com.example.dashboard_our.hmPlace"); 

這是代碼打印列表的其餘部分:

final StableArrayAdapter adapter = new StableArrayAdapter(this, 
       android.R.layout.simple_list_item_1, list); 
      listview.setAdapter(adapter); 

      listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, final View view, 
        int position, long id) { 
       final String item = (String) parent.getItemAtPosition(position); 
       view.animate().setDuration(2000).alpha(0) 
        .withEndAction(new Runnable() { 
         @Override 
         public void run() { 
         list.remove(item); 
         adapter.notifyDataSetChanged(); 
         view.setAlpha(1); 
         } 
        }); 
       } 

      }); 
      } 

      private class StableArrayAdapter extends ArrayAdapter<String> { 

      HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); 

      public StableArrayAdapter(Context context, int textViewResourceId, 
       List<String> objects) { 
       super(context, textViewResourceId, objects); 
       for (int i = 0; i < objects.size(); ++i) { 
       mIdMap.put(objects.get(i), i); 
       } 
      } 

      @Override 
      public long getItemId(int position) { 
       String item = getItem(position); 
       return mIdMap.get(item); 
      } 

      @Override 
      public boolean hasStableIds() { 
       return true; 
      } 

      } 

與此代碼中的第一個活動*強調文本*

protected void onPostExecute(List<HashMap<String,String>> list){ 

       // Clears all the existing markers 
       mGoogleMap.clear(); 

       for(int i=0;i<list.size();i++){ 

        // Creating a marker 
        MarkerOptions markerOptions = new MarkerOptions(); 

        // Getting a place from the places list 
        //HashMap<String, String> 
        hmPlace = list.get(i); 

        // Getting latitude of the place 
        double lat = Double.parseDouble(hmPlace.get("lat")); 

        // Getting longitude of the place 
        double lng = Double.parseDouble(hmPlace.get("lng")); 

        // Getting name 
        String name = hmPlace.get("place_name"); 

        // listP[i]=hmPlace.get("place_name"); 
        Log.d("places=",hmPlace.get("place_name")); 

        // Getting vicinity 
        String vicinity = hmPlace.get("vicinity"); 

        LatLng latLng = new LatLng(lat, lng); 

        // Setting the position for the marker 
        markerOptions.position(latLng); 

        // Setting the title for the marker. 
        //This will be displayed on taping the marker 
        markerOptions.title(name + " : " + vicinity); 

        // Placing a marker on the touched position 
        Marker m = mGoogleMap.addMarker(markerOptions); 

        // Linking Marker id and place reference 
        mMarkerPlaceLink.put(m.getId(), hmPlace.get("reference")); 
       } 
      } 
     } 

我將hmPlace傳遞給第二個活動,如下所示:

intent = new Intent(getApplicationContext(), List_airports.class); 
       intent.putExtra("com.example.dashboard_our.hmPlace",hmPlace); 

       startActivity(intent); 
+0

如果你拔去循環,只要把自己的價值觀列表,'list.addAll(places1.values());'它會工作 – kiruwka

+0

@ kiruwka它打印關於第一個元素的信息 –

+0

你可以發佈打印的代碼嗎?如果你輸出'System.out.println(「myMap =」+ places1)',你會看到你的地圖內容(或放置一個斷點)。它可能包含所有鍵的相同值 – kiruwka

回答

1

迭代通過的entrySet:

public static void printMap(Map mp) { 
    Iterator it = mp.entrySet().iterator(); 
    while (it.hasNext()) { 
     Map.Entry pairs = (Map.Entry)it.next(); 
     System.out.println(pairs.getKey() + " = " + pairs.getValue()); 
     it.remove(); // avoids a ConcurrentModificationException 
    } 
} 
+0

哪裏是HashMap? –

+0

將它作爲函數參數傳遞給printMap。像printMap(mIdMap); – TheGeekNess

+0

同樣的問題,它只打印有關第一個項目的信息,請問您可以檢查我的問題編輯 –

0

可以遍歷的地方,像這樣:

for (String place : places1.values()) { 
    list.add(place); 
} 
+0

它會多次打印第一個元素信息 –

0

您可以使用HashMap.values()方法,它返回Collection<V>//where V is the value type

final ArrayList<String> list = new ArrayList<String>(places1.values()); 

或本:ArrayList.addAll(Collection<? extends E> collection)

list.addAll(places1.values());// Only call this once!