listitems is returning null, from a ParseQuery

  • Replies:0
Aashif Smith
  • Forum posts: 1

Feb 15, 2017, 9:31:57 PM via Website

Good Day,

In this snippet of code, I'm making a ParseQuery to retrieve objects based on a condition. When I place a break point at the beginning of the for block, within the done method, I can see the number of objects it has, that I want to retrieve, as I step through, each item is being added to the listItems array, but when I fall through and hit the final breakpoint @return listItems the listitems array is null.

I tried to follow some examples from the response, from SO, but can't seem to figure it out. In the call back it gets the objects, but I don't know how to assign the results to the listitems object.

Could someone tell me what I am doing wrong here, I tracked my problem down to this snippet of code.

public List<ListItem> getListItems()

{
final List listItems = new ArrayList();

ParseUser currentUser = ParseUser.getCurrentUser();
ParseQuery<ParseObject> postQuery = ParseQuery.getQuery("posts");

postQuery.whereEqualTo("username", currentUser.getUsername());
postQuery.findInBackground(new FindCallback<ParseObject>() {
    @Override
    public void done(List<ParseObject> objects, ParseException e) {
        if (e == null) {
            for (ParseObject object : objects)  {
                ListItem item = new ListItem();
                ParseFile imgFile = object.getParseFile("pic");
                item.setPostImage(imgFile);
                listItems.add(item);
            }
        }
    }
});

return listItems;

}

Reply