最后输入的HashMap键是为地图中的每个元素设置的

时间:2015-04-17 23:47:52

标签: java android xml hashmap

我正在使用XMLPullParser将我的XML解析为hashmap。我的XML是一个“菜单”,hashmap的键是类别,值是菜单项的ArrayList。但是,在HashMap中,输入的最后一个键是HashMap中所有项的键,而不仅仅是最后一个元素。为什么是这样?这是我的代码:

   public HashMap<String, ArrayList<String>> getMenu() throws IOException,
        XmlPullParserException {

    XmlPullParser xpp = appContext.getResources().getXml(R.xml.menu);

    HashMap<String, ArrayList<String>> menuItems = new HashMap<String, ArrayList<String>>();
    ArrayList<String> items = new ArrayList<String>();
    String catName = null;
    while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
        if (xpp.getEventType() == XmlPullParser.START_TAG) {
            if (xpp.getName().equals("categoryname")) {
                catName = xpp.nextText();
                menuItems = new HashMap<String, ArrayList<String>>();
            } else if (xpp.getName().equals("item")) {
                items.add(xpp.nextText());
            }
        } else if (xpp.getEventType() == XmlPullParser.END_TAG) {
            if (xpp.getName().equals("category")) {
                System.out.println("Adding Category: " + catName);
                menuItems.put(catName, items);
                catName = new String();
            }
        }

        xpp.next();
    }

    for (Map.Entry<String, ArrayList<String>> entry : menuItems.entrySet()) {
        String key = entry.getKey();
        ArrayList<String> value = entry.getValue();
        for(String str : value) {
            System.out.println("Category: " + key + " Item: " + str);
        }
    }

    return menuItems;
}

我正在解析的XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<restaurant>
   <restaurantname>Arbys</restaurantname>
   <category>
      <categoryname>Roast Beef Sanwiches</categoryname>
      <item>Sandwich</item>
      <item>Melt</item>
   </category>
   <category>
      <categoryname>Beverages</categoryname>
      <item>Brewed Iced Tea</item>
   </category>
   <category>
      <categoryname>Breakfast</categoryname>
      <item>Bacon, Egg &amp; Cheese Biscuit</item>
      <item>Chicken Biscuit</item>
   </category>
   <category>
      <categoryname>Chicken</categoryname>
      <item>Crispy Chicken Sandwich</item>
      <item>Prime-Cut Chicken Tenders</item>
   </category>
</restaurant>

我的日志如下:

System.out(7153): Adding Category: Roast Beef Sanwiches
System.out(7153): Adding Category: Beverages
System.out(7153): Adding Category: Breakfast
System.out(7153): Adding Category: Chicken
System.out(6523): Category: Chicken Item: Sandwich
System.out(6523): Category: Chicken Item: Melt
System.out(6523): Category: Chicken Item: Brewed Iced Tea
System.out(6523): Category: Chicken Item: Bacon, Egg & Cheese Biscuit
System.out(6523): Category: Chicken Item: Chicken Biscuit
System.out(6523): Category: Chicken Item: Crispy Chicken Sandwich
System.out(6523): Category: Chicken Item: Prime-Cut Chicken Tenders

所以,'鸡'是他们所有人的类别,但是,它应该只是最后2项的类别。谁知道怎么了?感谢

1 个答案:

答案 0 :(得分:1)

这是因为两件事:每次找到名为menuitems的标签时,循环都会为categoryname创建新对象,并且所有项目都会被添加到同一items列表中。所以最后你有一个menuitems地图被创建了最后找到的类别,它正在添加所有项目,因为它们都被添加到同一个列表中。