所以我现在正在使用Parse来运行我的应用程序的后端。当您查询Parse数据库时,数据以一种看似奇怪的方式返回(对我来说,但我是新手) -
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
} else {
}
}
});
查询中的数据可以在Else语句中找到。我可以做Date time = object.getCreatedAt();获取在...创建检索对象的时间。
但是,问题出现了,因为我想使用该数据更新我的应用程序中的textView文本。我觉得数据被“卡住”在callBack中(我意识到这可能不是真的。我试图这样做的方式我得到一个错误:“不能引用内部类定义的非最终变量updateText用不同的方法“。错误发生在updateText,timeText,table和i。这是失败的相关代码片段 -
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));
}
}
});
}
任何帮助都将不胜感激。
答案 0 :(得分:0)
我现在无法测试它,但是这样的事情应该有效:
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
)被声明为final
,所以一切都应该有效。
答案 1 :(得分:0)
有两种方法可以解决这个问题。正如Darshan所说,其中之一就是让桌子最终成功。这将允许您从GetCallback对象(内部类)内部访问它而不会出现错误。第二种方法是引用回父对象 - 例如,如果此代码位于名为DisplayTableActivity
的Activity子类中,则可以将方法放入UpdateTable(String updateText, String timeText)
,而不是访问表直接在GetCallback对象中,有以下行:
DisplayTableActivity.this.UpdateTable(updateText, timeText);
这样,您就可以在该函数(属于Activity)中获取表格视图而不会出现任何问题。