gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:43:05 25 4
gpt4 key购买 nike

我已经搜索了几个小时,但似乎找不到我的错误,事实上它根本没有任何意义。

我有一个 Android 应用程序,它有一个类负责使用 XmlPullParser 解析 XML 文档。结果是一个上学日列表,其中包含有关当天的一些信息以及每天的类(class)列表。我能够正确提取所有这些信息,但由于某种原因,我最终每天都有相同的类(class)列表(尽管该元素的其他“标题”信息仍然正确)。完整的方法可以在这里找到(太长无法粘贴在这里):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),则结果会增加一,因为我将每个替换/类(class)存储在列表的一个元素中。最后一天的类(class)数是 5 - 这是复制到所有其他日子的数字。

最佳答案

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

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

关于android - 列表将自身复制到父列表的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42253454/

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