-1

ArrayList<> Duplicating the last value,,,

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

Th output display only last value

{"CREATED_DATE":"01-Jun-2012","Summary":"Today Swimming"},{"CREATED_DATE":"01-Jun-2012","Summary":"Today Swimming"},{"CREATED_DATE":"01-Jun-2012","Summary":"Today Swimming"}
Pablo
  • 3,655
  • 2
  • 30
  • 44

3 Answers3

2

You should put that inside the loop:

CalanderQueryOutput caldavOutput = new CalanderQueryOutput();

Else, you are always modifying the same caldavOutput object.

Your are always putting the same element and modifying it and the element is left with the last values that you gave it. That's why you think that the last element only is output.

So you should do:

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); 
}
Mesop
  • 5,187
  • 4
  • 25
  • 42
2

The problem is that you only create one CalenderQueryOutput which you modify and read to the list. You need to create a new object.

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); 
}
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
  • I am not modifying only listing i CalenderQueryOutput – Vignesh Kumar Chandhrasekaran Jun 04 '12 at 07:06
  • The caldavOutput.CREATED_DATE = Created; is modifying the object. What you originally did was create one object and add it to the list, then modify that object (thus modifying the object already in the list) and then add ti to the list again. Now the list has two references, both pointing to the same object. In the end you had a list of n references, all pointing to the same object which of course had the values of the last modification. – Roger Lindsjö Jun 04 '12 at 07:09
1

Put this inside for loop....

CalanderQueryOutput caldavOutput = new CalanderQueryOutput();
Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29