So I’m using Parse for now to run the backend of my app. When you query your Parse database, data is returned in a seemingly weird way (to me, but I’m new to this) –
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
} else {
}
}
});
And the data from the query is available for example within the Else statement. I can do Date time = object.getCreatedAt(); to get the time the retrieved object was created at.
But, the problem comes about because I want to use that data to update the text of a textView within my app. I feel like the data is “stuck” inside the callBack (which I realize is probably not true. The way I’m trying to do it I get an error: “Cannot refer to a non-final variable updateText inside an inner class defined in a different method”. The error is on updateText, timeText, table, and i. Here is the relevant code snippet that is failing –
TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TextView courtText;
TextView updateText;
TextView timeText;
// For each row
for (int i = 0; i < table.getChildCount(); i++) {
// Get the one `courtText` in this row
courtText = (TextView) table.getChildAt(i).findViewById(R.id.courtText);
ParseQuery query = new ParseQuery("Updates");
query.whereEqualTo("court",courtText);
query.orderByAscending("createdAt");
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("update", "The getFirst request failed.");
} else {
Log.d("update", "Retrieved the object.");
String update = object.getString("update");
Date time = object.getCreatedAt();
updateText = (TextView) table.getChildAt(i).findViewById(R.id.updateText);
timeText = (TextView) table.getChildAt(i).findViewById(R.id.timeText);
updateText.setText(update);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
timeText.setText(dateFormat.format(time));
}
}
});
}
Any help would be appreciated.
I’m not able to test it right now, but something like this should work:
This way the only two variables from outside the inner class that need to be accessed from inside it (
updateTextandtimeText) are declaredfinal, so everything should work.