0

正如您在下面看到的是我的 RSS 解析器,我想在 ListView 中显示输出。但是如何将列表类型的变量“标题”和“链接”提取到字符串中,以便我可以添加到我的列表项中?

ListItem listItem = new ListItem();
headlines = new ArrayList();
links = new ArrayList();

try {
    URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();

    // We will get the XML from an input stream
    xpp.setInput(getInputStream(url), "UTF_8");

    /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
    * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
    * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
    * so we should skip the "<title>" tag which is a child of "<channel>" tag,
    * and take in consideration only "<title>" tag which is a child of "<item>"
    *
    * In order to achieve this, we will make use of a boolean variable.
    */
    boolean insideItem = false;

    // Returns the type of current event: START_TAG, END_TAG, etc..
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {

            if (xpp.getName().equalsIgnoreCase("item")) {
                insideItem = true;
            } else if (xpp.getName().equalsIgnoreCase("title")) {
                if (insideItem)
                    headlines.add(xpp.nextText());                         //extract the headline

            } else if (xpp.getName().equalsIgnoreCase("link")) {
                if (insideItem)
                    links.add(xpp.nextText());                            //extract the link of article
            }
        } else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
            insideItem=false;
        }

        eventType = xpp.next(); //move to next element
    }

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



listItem.heading = ?? ;
listItem.content = ?? ;
listItemList.add(listItem);
4

1 回答 1

0

在不同的活动中创建一个新类,声明您要附加到列表视图的所有变量,并为这些变量编写 get 和 set 方法。

如果要添加到字符串值,请使用 settext() / 获取 gettext()

于 2013-09-12T11:40:14.620 回答