gpt4 book ai didi

android - 如何通过解析XML获取属性和图片

转载 作者:行者123 更新时间:2023-11-30 01:36:51 25 4
gpt4 key购买 nike

我正在使用 Android Studio 创建应用。我有这样的 XML 数据:

XML Data

我希望我的第一个 Activity 显示员工姓名列表。当我单击任何名称时,我希望我的第二个 Activity 根据我单击的名称显示图像列表。我该怎么做?

最佳答案

Android 的 XmlPullParser在这方面会有很大用处。

There are two key methods: next() and nextToken(). While next() provides access >to high level parsing events, nextToken() allows access to lower level tokens.

The current event state of the parser can be determined by calling the >getEventType() method. Initially, the parser is in the START_DOCUMENT state.

The method next() advances the parser to the next event. The int value returned >from next determines the current parser state and is identical to the value >returned from following calls to getEventType ().

这是来自上面链接的示例:

public static void main (String args[])
throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
}

上面的问题有点宽泛,但我会尝试提供一些关于如何做的线索:

您应该遍历数据,创建员工列表 POJOs ,并在你的 ListView's 中使用它适配器。您还可以设置 on click listener对于每个项目,然后使用 an imageview显示图像。

关于android - 如何通过解析XML获取属性和图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com