Search code examples
androidarraylistxmlpullparser

List copies itself to all elements of the parent list


I've been searching for hours and I can't seem to find my error, in fact it doesn't make any sense at all.

I have a android app which has a class that is responsible for parsing an XML document using the XmlPullParser. The result is a list of school days that holds some information about the day and also a list of lessons for each day. I am able to extract all those information correctly but for some reason I end up having the same list of lessons for every day in the end (the other 'header' information of that element remain correct though). Complete method can be found here(too long to paste it here): http://pastebin.com/AwxqwxQb

I just post the outline here:

 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;
}

I'm really helpless right here as this shouldn't happening at all. Does anyone have an explanation for that? Any help is appreciated. If there's further information needed, just let me know!

Edit: The XML that is parsed can be found here: http://vplankl.gymnasium-beetzendorf.de/Vertretungsplan_Klassen.xml

The results I'm expecting are basically the number of rows from each table. However if one row is for let's say two periods (column name: Stunde) (4-5 for example) the result is increased by one because I store each Substitution/Lesson in one element of the list. The lesson count for the last day is 5 - which is the number that is copied to all the other days.


Solution

  • The problem is that you only create one List<Substitution> object and keep clearing it out. Instead you should create a brand new list for each day.

    Additionally, you should make a defensive copy when you add the list to a SubstitutionDay.