使用XML分析显示ListView中的最后一项

时间:2017-09-08 10:00:11

标签: android listview xml-parsing listviewitem

我想在ListView中显示城市列表使用XML解析我在Logcat中得到响应但是在ListView中,只显示最后一项,我很多人试图解决这个问题,但我陷入其中。 请帮忙解决这个问题

我的XML解析代码如下所示

class XmlPullParserHandler {
    private List<City> cityArrayList = new ArrayList<City>();
    private City cities;
    private String text;

    public List<City> getCitiess() {
        return cities;
    }

    public List<City> parse(InputStream is) {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser parser = factory.newPullParser();

            parser.setInput(is, null);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if (tagname.equalsIgnoreCase("city")) {
                            // create a new instance of cities
                            cities = new City();
                        }
                        break;

                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;

                    case XmlPullParser.END_TAG:
                        if (tagname.equalsIgnoreCase("Area")) {
                            // add cities object to list
                            cityArrayList.add(cities);
                        } else if (tagname.equalsIgnoreCase("AreaName") {
                            cities.setAreaName(text);
                        } else if (tagname.equalsIgnoreCase("CityCode")) {
                            cities.setCityCode(text);
                        } else if (tagname.equalsIgnoreCase("AreaCode")) {
                            cities.setAreaCode(text);
                        }
                        break;

                    default:
                        break;
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cityArrayList;
    }
}

设置适配器代码在

之下
List<City> cities = null;
        try {
            XmlPullParserHandler parser = new XmlPullParserHandler();
            InputStream is = getAssets().open("cityapi/citylist.xml");
            cities = parser.parse(is);

            AdapterSearchCity adapter = new AdapterSearchCity(ActivityCitySearch.this, cities);
            searchListview.setAdapter(adapter);

        } catch (IOException e) {
            e.printStackTrace();
        }

我的xml如下所示

<city>
<Area>
<AreaName>L.A</AreaName>
<CityCode>ABC</CityCode>
<AreaCode>XYZ</AreaCode>
</Area>
</city>

我没有得到我错的地方请帮帮我

1 个答案:

答案 0 :(得分:0)

我找到了我错了的解决方案

我把“city”代替“Area”

应该是这样的。

case XmlPullParser.START_TAG:
                        if (tagname.equalsIgnoreCase("Area")) {
                            // create a new instance of cities
                            cities = new City();
                        }
                        break;