I'm having an embarrassing problem. For the life of me, I can't figure out why my code isn't working.
private void getParseObject(String title) {
final String parseTitle = title;
ParseQuery<ParseObject> query = ParseQuery.getQuery(title);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.isEmpty()){
createParse(parseTitle);
} else {
ParseObject event = objects.get(0);
int likes = event.getInt("likes");
JSONArray comments = event.getJSONArray("comments");
setParseInfo(likes, comments);
}
} else {
Log.v("Miles", String.valueOf(e.getCode()));
}
}
});
Log.v("Miles", "LIKES FROM GOT " + likes);
}
public void setParseInfo(int likesFromParse, JSONArray commentsFromParse) {
this.likes = likesFromParse;
this.comments = commentsFromParse;
Log.v("Miles", "LIKES FROM SET " + likes);
}
Within setParseInfo(int, JSONArray), I'm setting the global variable of my fragment. I can get the info fine, it's not null; In that log "LIKES FROM SET", the "likes" int appears more than normally. However, when I try to do the same within getParseObject() in that "LIKES FROM GOT" log, the "likes" int show up as 0. Any ideas on how I can fix this?
EDIT 1:
With the advice of Ryan J, I've tried doing something like this, and it has the same effect.
private void getParseObject(String title) {
final String parseTitle = title;
ParseQuery<ParseObject> query = ParseQuery.getQuery(title);
query.findInBackground(new FindCallback<ParseObject>() {
int nestedLikes = likes;
JSONArray nestedComments = comments;
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.isEmpty()){
createParse(parseTitle);
} else {
ParseObject event = objects.get(0);
nestedLikes = event.getInt("likes");
nestedComments = event.getJSONArray("comments");
setParseInfo(nestedLikes, nestedComments);
}
} else {
Log.v("Miles", String.valueOf(e.getCode()));
}
}
});
Log.v("Miles", "LIKES FROM GOT " + likes);
}
Which is what I think the suggestion was. However, the log "LIKES FROM GOT" still returns 0.
FindCallbackclass (thedonemethod). Have you verified the proper value is being output in that class via the debugger or printing to console? Create a global variable in your top-level class, and set it to the value of likes in your anonymous class. That hasn't worked for you?event.getInt("likes")gets the correct value. The problem is getting that value to be assigned to the fragment's top-level global variable.