的ArrayList<>复制最后一个值,,,
ArrayList<CalanderQueryOutput> results = new ArrayList<CalanderQueryOutput>();
List<?> eventsToday = (List<?>) filter.filter(calendar.getComponents(Component.VEVENT));
CalanderQueryOutput caldavOutput = new CalanderQueryOutput();
for (Iterator<?> i = eventsToday.iterator(); i.hasNext();) {
Component component = (Component) i.next();
{
String Created=component.getProperty(Property.CREATED).toString().trim();
String Summary = component.getProperty(Property.SUMMARY).toString().trim();
caldavOutput.CREATED_DATE = Created;
caldavOutput.Summary = Summary;
}
results.add(caldavOutput);
}
输出仅显示最后一个值
{"CREATED_DATE":"01-Jun-2012","Summary":"Today Swimming"},{"CREATED_DATE":"01-Jun-2012","Summary":"Today Swimming"},{"CREATED_DATE":"01-Jun-2012","Summary":"Today Swimming"}
答案 0 :(得分:2)
你应该把它放在循环中:
CalanderQueryOutput caldavOutput = new CalanderQueryOutput();
否则,您总是修改相同的caldavOutput
对象。
你始终使用相同的元素并对其进行修改,并且元素将保留您给出的最后一个值。这就是为什么你认为只输出最后一个元素。
所以你应该这样做:
for (Iterator<?> i = eventsToday.iterator(); i.hasNext();) {
Component component = (Component) i.next();{
String Created=component.getProperty(Property.CREATED).toString().trim();
String Summary = component.getProperty(Property.SUMMARY).toString().trim();
CalanderQueryOutput caldavOutput = new CalanderQueryOutput(); // Here
caldavOutput.CREATED_DATE = Created;
caldavOutput.Summary = Summary;
}
results.add(caldavOutput);
}
答案 1 :(得分:2)
问题是您只创建一个CalenderQueryOutput,您可以修改并读取该列表。您需要创建一个新对象。
for (Iterator<?> i = eventsToday.iterator(); i.hasNext();) {
Component component = (Component) i.next();
String Created=component.getProperty(Property.CREATED).toString().trim();
String Summary = component.getProperty(Property.SUMMARY).toString().trim();
CalanderQueryOutput caldavOutput = new CalanderQueryOutput();
caldavOutput.CREATED_DATE = Created;
caldavOutput.Summary = Summary;
results.add(caldavOutput);
}
答案 2 :(得分:1)
将其置于循环中....
CalanderQueryOutput caldavOutput = new CalanderQueryOutput();