2012-01-18 241 views
59

我對Android開發非常新,我試圖在Android示例項目中使用HashMap。現在,我正在做學習android的示例項目。我只是將鍵和值存儲在HashMap中,我想在EditView中顯示鍵和它們的值。我在我的示例項目中遵循以下代碼。但是,第一個鍵和值只能在EditView中打印。如何在Android中打印HashMap中的所有鍵和值?

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

在EditView中iOS = 100只是打印。我想要在EditText中打印所有的鍵和它們的值。任何人都可以告訴我我在哪裏做錯了?提前致謝。

+0

看到這個問題和其他許多人:http://stackoverflow.com/questions/1066589/ java-iterate-through-hashmap – Anton 2012-01-18 12:09:12

+0

你好Gopinath !!您獲取並設置哈希映射鍵值的代碼是正確的,但是您只將這些值設置爲一個textView。 – 2012-01-18 12:10:32

+1

謝謝大家。我得到了答案。我再一次感謝你們。 – Gopinath 2012-01-18 12:22:35

回答

7

首先,你的代碼有錯誤,即。您在for循環中缺少分號和右括號。

然後,如果您嘗試將值附加到視圖,則應該使用textview.appendText()而不是.setText()。

有一個類似的問題在這裏:how to change text in Android TextView

+2

這是最好的答案,但也請參閱@Shadow以瞭解如何正確使用Java 5 for和Map.Entry。 – 2012-01-18 12:12:41

164
for (Map.Entry<String,String> entry : map.entrySet()) { 
    String key = entry.getKey(); 
    String value = entry.getValue(); 
    // do stuff 
} 
+2

偉大的工作的人,你救了我:) – delive 2015-06-03 16:36:56

1
String text=""; 

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

     textview.setText(text); 
12

這是因爲你的TextView收到的每一個迭代和previuos價值扔掉的新文本。通過StringBuilder連接字符串並在循環後設置TextView值。 您也可以使用這種類型的循環:

for (Map.Entry<String, String> e : map.entrySet()) { 
    //to get key 
    e.getKey(); 
    //and to get value 
    e.getValue(); 
} 
2

該代碼進行測試和工作。

public void dumpMe(Map m) { dumpMe(m, ""); } 
private void dumpMe(Map m, String padding) { 
    Set s = m.keySet(); 
    java.util.Iterator ir = s.iterator(); 
    while (ir.hasNext()) { 
    String key = (String) ir.next(); 
    Object value = m.get(key); 
    if (value == null) continue; 
    if (value instanceof Map) { 
     System.out.println (padding + key + " = {"); 
     dumpMe((Map)value, padding + " "); 
     System.out.println(padding + "}");   
    } 
    else if (value instanceof String || 
      value instanceof Integer || 
      value instanceof Double || 
      value instanceof Float || 
      value instanceof Long) { 

     System.out.println(padding + key + " = " + value.toString()); 
    } 
    else { 
     System.out.println(padding + key + " = UNKNOWN OBJECT: " + value.toString()); 
     // You could also throw an exception here 
    }  
    } // while 

} // dumpme 

Charles。

+0

謝謝,添加到我的工具 – moberme 2014-04-11 23:20:06

1

您可以使用此代碼:

for (Object variableName: mapName.keySet()){ 
    variableKey += variableName + "\n"; 
    variableValue += mapName.get(variableName) + "\n"; 
} 
System.out.println(variableKey + variableValue); 

此代碼將確保所有的密鑰存儲在一個變量,然後打印!

12
HashMap <Integer,Integer> hm = new HashMap<Integer,Integer>(); 

Set<Integer> keys = hm.keySet(); //get all keys 
for(Integer i: keys) 
{ 
    System.out.println(hm.get(i)); 
} 
1
public void dumpMe(Map m) { dumpMe(m, ""); } 

private void dumpMe(Map m, String padding) 
{ 
    Set s = m.keySet(); 
    java.util.Iterator ir = s.iterator(); 
    while (ir.hasNext()) 
    { 
     String key = (String) ir.next(); 
     AttributeValue value = (AttributeValue)m.get(key); 
     if (value == null) 
      continue; 
     if (value.getM() != null) 
     { 
      System.out.println (padding + key + " = {"); 
      dumpMe((Map)value, padding + " "); 
      System.out.println(padding + "}");   
     } 
     else if (value.getS() != null || 
       value.getN() != null) 
     { 
      System.out.println(padding + key + " = " + value.toString()); 
     } 
     else 
     { 
      System.out.println(padding + key + " = UNKNOWN OBJECT: " + value.toString()); 
      // You could also throw an exception here 
     }  
    } // while 
} 

//This code worked for me. 
+0

請看看[回答] – JimHawkins 2016-09-03 05:34:19

3

與Java 8:

map.keySet().forEach(key -> System.out.println(key + "->" + result.get(key))); 
+0

你可以請[編輯]在解釋爲什麼這段代碼回答這個問題?僅限代碼答案[阻止](http://meta.stackexchange.com/q/148272/274165),因爲他們沒有教導解決方案。 – 2016-11-02 23:25:33

+0

OP想要打印所有的鍵和值對。我不認爲這裏需要任何解釋。這是一條非常古老的線索,已經回答了,也沒有腦子的東西。我已經寫過,如果有人在Java8中尋找解決方案,他/她可以這樣做。 – mdev 2016-11-04 01:37:28

4

的Java 8

map.entrySet().forEach(System.out::println); 
2

你可以做到這一點與GSON簡單:

Log.i(TAG, "SomeText: " + new Gson().toJson(yourMap)); 

結果看起來像:

I/YOURTAG: SomeText: {"key1":"value1","key2":"value2"} 
0

大家誰點擊這個來找出你的HashMap的內容是什麼,toString方法(docs)實際上大多數對象的作品。 (注:Java數組是不是對象!)

所以這鑊完美的罰款調試目的:

System.out.println(myMap.toString()); 

>>> {key1=value1, key2=value2}