如何使用getCallback以外在一个getCallback提供的数据 [英] How to use data provided in a getCallback outside of the getCallback
问题描述
所以我使用的解析现在运行我的应用程序的后端。当您查询您解析数据库,数据是在一个看似不可思议的方式返回(给我,但我是新来的这一点) -
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 {
}
}
});
和从查询的数据可用于例如Else语句内。我能做的日期时间= object.getCreatedAt();拿到检索对象的创建时间。
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.
但是,问题来约,因为我想用这些数据来更新一个TextView的我的应用程序中的文本。我觉得像数据是卡住回调(这我知道里面很可能不是真正的我想的办法做到这一点,我得到一个错误:不能指非final的变量UPDATETEXT定义的内部类中。在不同的方法,该错误是在UPDATETEXT,timeText,表,我这里是没有相关的code片段 -
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));
}
}
});
}
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
我不能现在就测试,但这样的事情应该工作:
I'm not able to test it right now, but something like this should work:
final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TextView courtText;
for (int i = 0; i < table.getChildCount(); i++) {
courtText = (TextView) table.getChildAt(i).findViewById(R.id.courtText);
final TextView updateText = (TextView) table.getChildAt(i).findViewById(R.id.updateText);
final TextView timeText = (TextView) table.getChildAt(i).findViewById(R.id.timeText);
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.setText(update);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
timeText.setText(dateFormat.format(time));
}
}
});
}
此方式,从需要被从里面( UPDATETEXT
和 timeText $ C $访问内部类以外的唯一两个变量C>)的声明
最后
,所以一切都应该工作。
This way the only two variables from outside the inner class that need to be accessed from inside it (updateText
and timeText
) are declared final
, so everything should work.
这篇关于如何使用getCallback以外在一个getCallback提供的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!