java android获取HashMap数组的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13250160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:02:11  来源:igfitidea点击:

android get value of HashMap array

javaandroidhashmapkey-value

提问by coldwind

Im trying to get value of Hashmap array but im getting index/key.

我试图获取 Hashmap 数组的值,但我正在获取索引/键。

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

Code how i get values from parser

编码我如何从解析器获取值

 HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_LAT, parser.getValue(e, KEY_LAT));
map.put(KEY_LON, parser.getValue(e, KEY_LON));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
menuItems.add(map);

Then i try get values but i receive index.

然后我尝试获取值但我收到索引。

for (int i = 0; i < menuItems.size(); i++){
        int latitude = Integer.parseInt(KEY_LAT);
        int longitude = Integer.parseInt(KEY_LON);
        itemizedOverlay.addOverlayItem(latitude, longitude, KEY_NAME, makerDefault);
    }

How to get value from menuItems array ? In other activity i get values but there is used through TextView. Is there other way ?

如何从 menuItems 数组中获取值?在其他活动中,我获得了值,但通过 TextView 使用。还有其他方法吗?

回答by Bohemian

Try this:

试试这个:

for (Map<String, String> menuItem : menuItems) {
    int latitude = Integer.parseInt(menuItem.get(KEY_LAT));
    int longitude = Integer.parseInt(menuItem.get(KEY_LON));
    itemizedOverlay.addOverlayItem(latitude, longitude, KEY_NAME, makerDefault);
}

回答by Drejc

Iterating through the Array is best done like this:

遍历数组最好是这样完成的:

for (HashMap map: menuItems) {
}

then you can access the map itself:

然后您可以访问地图本身:

for (HashMap map: menuItems) {
    String value = map.get(key);
}