0

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

}
1
  • Use Log instead of print for debugging in android. Post your log after changing. Commented Jul 30, 2019 at 1:31

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.