【问题标题】:android get value of HashMap arrayandroid获取HashMap数组的值
【发布时间】:2012-11-07 03:39:27
【问题描述】:

我正在尝试获取 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 使用。还有其他方法吗?

【问题讨论】:

    标签: java android hashmap key-value


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

      遍历数组最好是这样:

      for (HashMap map: menuItems) {
      }
      

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

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

      【讨论】:

        猜你喜欢
        • 2020-11-30
        • 2011-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-22
        • 2014-11-25
        相关资源
        最近更新 更多