简体   繁体   English

如何获取存储在ArrayList中的值 <HashMap<key,value> &gt;?

[英]How to get value stored in ArrayList<HashMap<key,value>>?

I have ArrayList>. 我有ArrayList>。 In another activity I want to access all values stored in ArrayList>. 在另一个活动中,我想访问存储在ArrayList>中的所有值。

I have tried following code: 我试过以下代码:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

for(Hashmap<String, String> map: mylist) {
    for(Entry<String, String> mapEntry: map) {
        String key = mapEntry.getKey();
        String value = mapEntry.getValue();
    }
}

but it shows an error at for(Entry<String, String> mapEntry: map) that it only interate over Array. 但它在for(Entry<String, String> mapEntry: map)中显示错误,它只与Array进行交互。

Your code has bit different for this line, 你的代码对于这一行有点不同,

for(Entry<String, String> mapEntry: map.entrySet())

Try this and let me know what happen, 试试这个,让我知道发生了什么,

for (HashMap<String, String> map : mylist)
     for (Entry<String, String> mapEntry : map.entrySet())
        {
        String key = mapEntry.getKey();
        String value = mapEntry.getValue();
        }

Simple way 简单的方法

Try this i hope it works for u also... 试试这个我希望它对你也有用......

ArrayList<HashMap<String,String>> arraylist;
for (int i=0;i<arraylist.size();i++)
    {
        HashMap<String, String> hashmap= arraylist.get(i);
        String string= hashmap.get("Your_Key_Name");
    }

Try this instead: 试试这个:

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    for(HashMap<String, String> map: mylist) {
        for(Entry<String, String> mapEntry: map.entrySet()) {
            String key = mapEntry.getKey();
            String value = mapEntry.getValue();
        }
    }

Note the line that says for(Entry<String, String> mapEntry: map.entrySet()) 注意for(Entry<String, String> mapEntry: map.entrySet())

According to this thread : Iterate through a HashMap you have to use HashMap.entrySet() method. 根据这个线程: 通过HashMap迭代你必须使用HashMap.entrySet()方法。

You can take a look here too : http://developer.android.com/reference/java/util/HashMap.html 您也可以在这里查看: http//developer.android.com/reference/java/util/HashMap.html

try this 试试这个

       for(HashMap<String,String> map:myList){
           for(String str:map.keySet()){
              String key=str;
              String value=map.get(str);
           }
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM