Reputation: 89
I'm a beginner so I feel like I might be making a silly mistake. Basically, I am running through a for loop and with each iteration, it is creating a new Event object and adding it to the arraylist called eventsList.
I have added a print statement at the bottom of each for loop iteration and it tells me that the size of eventsList is increasing: 1,2,3..
but for the second print statement, out of the for loop it tells me that the size is 0 (the second print statement which says: "size is...")
ListView eventsListView;
ArrayList<Event> eventList = new ArrayList<Event>();
Event event = null;
public void update() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Events");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException exception) {
if (exception != null) {
return
}
if (objects.size() == 0) {
System.out.println("objects empty");
return
}
System.out.println("objects size > 0");
for (ParseObject object: objects) {
String address = object.getString("address");
String name = object.getString("name");
String date = object.getString("date");
String time = object.getString("time");
String description =
object.getString("description");
event = new Event(name, date, address);
eventList.add(event);
System.out.println(eventList.size());
}
}
});
System.out.println("The size is: " + eventList.size());
}
Upvotes: 0
Views: 90
Reputation: 521008
There is nothing obviously wrong with your code, but the issue here is that when the The size is:
print statement is hit, the background task which actually populates the event list may not yet have completed. So, the change you need to make to your code is a logical one, in which you only attempt to use the populated list after the background task has finished. The place to do that is in the done()
method.
Upvotes: 3