列表将自身复制到父列表的所有元素

时间:2017-02-15 15:25:43

标签: android arraylist xmlpullparser

我一直在寻找几个小时,我似乎无法找到我的错误,事实上它根本没有任何意义。

我有一个Android应用程序,它有一个类,负责使用XmlPullParser解析XML文档。结果是一个上学日列表,其中包含有关当天的一些信息以及每天的课程列表。我能够正确地提取所有这些信息,但由于某种原因,我最终每天都有相同的课程列表(尽管该元素的其他“标题”信息仍然正确)。 可以在此处找到完整的方法(在此处粘贴太长时间):http://pastebin.com/AwxqwxQb

我只是在这里发布大纲:

 List<SubstitutionDay> parseReturnSubstitution() {
    SubstitutionDay currentSubstitutionDay = new SubstitutionDay();
    List<SubstitutionDay> results = new ArrayList<>();
    Substitution currentSubstitution = new Substitution();
    List<Substitution> currentSubstitutionList = new ArrayList<>();
    int multipleClasses = 0, multiplePeriods = 0;
    String text = "";

    try {
        // all the XmlPullParser set up

        int eventType = xmlPullParser.getEventType();

        while (eventType != END_DOCUMENT) {
            String tag;
            String[] tempStringArray;

            tag = xmlPullParser.getName();
            switch (eventType) {
                case TEXT:
                    text = xmlPullParser.getText();
                    break;

                case START_TAG:
                    switch (tag) {
                        case "kopf":
                            // reset the school day
                            currentSubstitutionDay = new SubstitutionDay();
                            break;
                        case "haupt":
                            // empty the list before new elements are added
                            currentSubstitutionList.clear();
                            break;
                        case "aktion":
                            // reset values for each new substitution
                            currentSubstitution = new Substitution();
                            multipleClasses = 0;
                            multiplePeriods = 0;
                            break;
                    }
                    break;

                case END_TAG:
                    switch (tag) {
                        // iterate over xml elements that contain header information about the day

                        // iterate over the individual lessons
                        case "klasse":

                            break;
                        case "stunde":

                            break;
                        case "lehrer":

                            break;
                        case "raum":

                            break;
                        case "info":

                            break;

                        case "aktion":

                            break;

                        case "haupt":
                            // add the list of lessons to the day
                            // the number of lessons is correct here
                            currentSubstitutionDay.setSubstitutionList(currentSubstitutionList);
                            // add the whole day to the result set
                            // number of lessons is still correct
                            results.add(currentSubstitutionDay);

                            break;
                    }
                    break;
            }
            eventType = xmlPullParser.next();
        }
    } // catch statements follow here

    // when I check for the size of results.get(i).getSubstitutionList().size() the size is the same for all elements
    return results;
}

我在这里真的很无助,因为这根本不应该发生。有没有人对此有解释?任何帮助表示赞赏。如果需要进一步的信息,请告诉我!

编辑:可以在此处找到已解析的XML:http://vplankl.gymnasium-beetzendorf.de/Vertretungsplan_Klassen.xml

我期待的结果基本上是每个表的行数。但是,如果一行代表两个句点(列名:Stunde)(例如4-5),则结果会增加一,因为我将每个Substitution / Lesson存储在列表的一个元素中。 最后一天的课程计数是5 - 这是复制到所有其他日期的数字。

1 个答案:

答案 0 :(得分:1)

问题是您只创建一个List<Substitution>对象并继续清除它。相反,你应该为每一天创建一个全新的列表。

此外,当您将列表添加到SubstitutionDay时,您应该制作防御性副本。