1

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

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

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

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

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

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

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

4

2 回答 2

4

试试这个:

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);
}
于 2012-11-06T11:50:55.317 回答
0

遍历 Array 最好这样做:

for (HashMap map: menuItems) {
}

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

for (HashMap map: menuItems) {
    String value = map.get(key);
}
于 2012-11-06T11:46:07.780 回答