名单将自身复制到我一直在寻找了几个小时,我似乎无法找到我的错误父列表
的所有元素,其实它没有任何意义可言。名单将自身复制到我一直在寻找了几个小时,我似乎无法找到我的错误父列表
我有有一个类是负责解析使用XmlPullParser一个XML文档中的Android应用程序。结果是一个学校日的列表,其中包含有关当天的一些信息以及每天的课程列表。我能够正确地提取所有这些信息,但由于某种原因,我最终每天都会得到相同的课程列表(该元素的另一个“标题”信息保持正确)。 完整的方法可以在这里找到(太长,粘贴到此处):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),结果会增加1,因为我将每个替代/课程存储在列表的一个元素中。 最后一天的课程数量是5 - 这是复制到其他日子的数字。
的问题是,你只能创建一个List<Substitution>
对象,并保持其清除出去。相反,您应该为每一天创建一个全新的列表。
此外,当您添加列表到SubstitutionDay
你应该做一个防守副本。
而不是清除,创建一个新的解决它 - 这是有道理的。谢谢! – Crosswind
我建议你使用调试器。很可能您对所有列表使用相同的参考,因为您应该为每个列表分别提供参考。 –
哪个班级适合“上课时间”,哪个班级适合“上课”? –
这就是我在做持续两个小时.. SubstitutionDay是学生时代,拥有的替代(教训)的列表 – Crosswind