How to print all key and values from HashMap in Android?

Gopinath picture Gopinath · Jan 18, 2012 · Viewed 240.6k times · Source

I am very new for Android development, and I am trying to use HashMap in Android sample project. Now, am doing sample project for learn android. I just store keys and values in HashMap, i want to show the keys and their values in EditView. I followed below code in my sample project. But, first key and value only printing in EditView.

   Map<String, String> map = new HashMap<String,String>();
   map.put("iOS", "100");
   map.put("Android", "101");
   map.put("Java", "102");
   map.put(".Net", "103");

   Set keys = map.keySet();

   for (Iterator i = keys.iterator(); i.hasNext(); ) {
       String key = (String) i.next();
       String value = (String) map.get(key);
       textview.setText(key + " = " + value);
   }

In EditView iOS = 100 is only printing. I want to print all keys and their value in EditText. Can anyone please tell me where i am doing wrong? Thanks in advance.

Answer

Shadow picture Shadow · Jan 18, 2012
for (Map.Entry<String,String> entry : map.entrySet()) {
  String key = entry.getKey();
  String value = entry.getValue();
  // do stuff
}