简体   繁体   English

如何将一个子元素值放入 Android 中的 ArrayList 中?

[英]How can a put a one child element value into an ArrayList in Android?

I have difficulties about the XmlPullParser event which is tagging all the elements but how can I store one child element into ArrayList?我对标记所有元素的 XmlPullParser 事件有困难,但是如何将一个子元素存储到 ArrayList 中?

this is my xml file:这是我的 xml 文件:

<site>
    <name>Brake and Break</name>
    <link>https://sampleactivity.000webhostapp.com/lesson_1.mp3</link>
        <set>When teaching my daughter how to drive, I told her if she didn't hit the brake in time she would break the car's side mirror.</set>
        <set>You shouldn’t brake as often as you do. You are going to break your car’s brakes.</set>
        <set>I think this relationship is moving too fast for me. Maybe it's best to just hit the brake than to completely break up.</set>
    <image>https://sampleactivity.000webhostapp.com/stckOvflw.png</image>
</site>

In my xml file I have element name SET so how can a store all the set elements and attributes into an array list?在我的 xml 文件中,我有元素名称SET那么如何将所有设置的元素和属性存储到数组列表中?

here is my xmlpullparser.java code:这是我的 xmlpullparser.java 代码:

switch (eventType) {
                case XmlPullParser.START_TAG:

                    if (tagname.equalsIgnoreCase(KEY_SITE)) {

                        curStackSite = new StackSite();
                    }
                    break;


                case XmlPullParser.TEXT:

                    curText = xpp.getText();
                    break;


                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase(KEY_SITE)) {

                        stackSites.add(curStackSite);
                    } else if (tagname.equalsIgnoreCase(KEY_NAME)) {

                        curStackSite.setName(curText);
                    } else if (tagname.equalsIgnoreCase(KEY_LINK)) {

                        curStackSite.setLink(curText);
                    } else if (tagname.equalsIgnoreCase(KEY_SET)) {
                        ArrayList<String> wew = new ArrayList<String>();
                        wew.add(curText);
                        curStackSite.setAlAbout(wew); <--- How can I store all element to arraylist here?
                        curStackSite.setSet(curText);
                    } else if (tagname.equalsIgnoreCase(KEY_IMAGE_URL)) {

                        curStackSite.setImgUrl(curText);
                    }
                    break;

                default:
                    break;
            }

i have tried to us dom parsing but still doesn't work much really in my code.我已经尝试过对我们进行 dom 解析,但在我的代码中仍然没有太大的作用。

UPDATE:更新:

this is my stacksite value:这是我的堆栈站点值:

public class StackSite {
private String name;
private String link;
private String about;
private List<String> AlAbout;
private String id;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String getLink() {
    return link;
}
public void setLink(String link) {
    this.link = link;
}

public String getAbout() {
    return about;
}
public void setAbout(String about) {this.about = about;}

public List<String> getAlAbout() {
    return AlAbout;
}
public void setAlAbout(List<String> AlAbout) {this.AlAbout = AlAbout;}

@Override
public String toString() {
    return "StackSite [name=" + name + ", link=" + link + ", about="
            + about + ", Array sets=" + AlAbout + "]";
}
}

If the StackSite class had a member as such:如果StackSite class 有这样的成员:

List<String> allSets;

and the constructor of StackSite initialized allSets to an empty list: allSets的构造函数将StackSite初始化为一个空列表:

allSets = new ArrayList<>();

then in the END_TAG case where tagName.equalsIgnoreCase(KEY_SET) you'd simply add the set text:然后在END_TAG情况下tagName.equalsIgnoreCase(KEY_SET)您只需添加设置文本:

curStackSite.allSets.add(curText);

would add a separate entry in the allSets list for each <set>…</set> encountered.将为遇到的每个<set>…</set>allSets列表中添加一个单独的条目。

At the end of parsing the allSets list member of StackSite contains 0 or more entries for set contents.在解析StackSiteallSets列表成员结束时,包含 0 个或多个集合内容条目。

As for attributes you'd probably want to create a new class for a StackSiteSet which contains the String set text and attributes fields rather than treating sets as just the String.至于属性,您可能希望为包含String集文本和属性字段的StackSiteSet创建一个新的 class,而不是将集合视为字符串。

So your END_TAG processing for </set> should look like:因此,您对</set>END_TAG处理应如下所示:

} else if (tagname.equalsIgnoreCase(KEY_SET)) {
    // Get the list and add another element to it.
    curStackSite.getAlAbout().add(curText);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM