1

I want to make a for loop to get the values from a HashMap. I tried this:

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

                list.addAll(places1.values());

            }

but It only brings the first element and print it many times, how can I do that??

where places1 is HashMap I got from another activity :

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

this is the rest of code that prints the list:

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

          }

and this the code in the first activity*emphasized text*

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"));
                }
            }
        }

I passed hmPlace to the second activity like this:

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

                startActivity(intent);
Krease
  • 15,805
  • 8
  • 54
  • 86
roa.tah
  • 547
  • 2
  • 11
  • 25
  • if you remove your loop, and just put your values in list as `list.addAll(places1.values());` it will work – kiruwka Nov 26 '13 at 20:45
  • @kiruwka it prints the info about the first element – roa.tah Nov 26 '13 at 20:48
  • can you post the code that prints ? if you output `System.out.println("myMap = " + places1)` you will see your map content (or put a breakpoint). It is possible it contains same value for all keys – kiruwka Nov 26 '13 at 20:50
  • @kiruwka places1 contains info about places on a map, I passed it from another activity – roa.tah Nov 26 '13 at 20:52
  • I understand what you think it contains. Can you post the code that prints the info ? Can you post the content of your map before you add values to the list ? – kiruwka Nov 26 '13 at 20:53
  • can you provide the content of your map `before` you call list.addAll ? – kiruwka Nov 26 '13 at 21:00
  • @kiruwka there is nothing before list.addAll, this code is at the beginning of the second activity, I want to get the values from the first activity. the first activity uses Google places API to get nearby locations and add information about these location in the HashMap, I want to get these locations in the second activity – roa.tah Nov 26 '13 at 21:06
  • I am just asking you to see the actual values of the map before you try to add them to list with your code. You could use debugger, or just print its content with `System.out.println("myMap = " + places1)`; I am pretty sure the problem is in the map, because addAll is the correct way to insert values and should work. – kiruwka Nov 26 '13 at 21:08
  • @kiruwka I will try this, but the places are shown on the map perfectly !! – roa.tah Nov 26 '13 at 21:10
  • @kiruwka the code uses Google play services, it can't be run on the emulator, so I can't see the output of printLn – roa.tah Nov 26 '13 at 21:13
  • You can get the output by println, if you enable LogCat. You can also debug via Wi-Fi. http://stackoverflow.com/questions/2604727/how-can-i-connect-to-android-with-adb-over-tcp – Sipka Nov 26 '13 at 21:48

3 Answers3

2

Iterate through the 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
    }
}
TheGeekNess
  • 1,607
  • 2
  • 10
  • 11
1

You can iterate over the places like so:

for (String place : places1.values()) {
    list.add(place);
}
Ricardo
  • 7,785
  • 8
  • 40
  • 60
0

You can use HashMap.values() method, it returns Collection<V>.//where V is the value type

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

or this: ArrayList.addAll(Collection<? extends E> collection)

list.addAll(places1.values());// Only call this once!
Sipka
  • 2,291
  • 2
  • 27
  • 30