0

所以我现在使用 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));
              }
            }
          });

    }

任何帮助,将不胜感激。

4

2 回答 2

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));
            }
        }
    });
}

这样,只有两个需要从内部访问的内部类外部变量(updateTexttimeText)被声明final,所以一切都应该工作。

于 2012-06-16T04:40:59.510 回答
0

There are two possible ways to resolve this. One, as Darshan said, is to make table final. This will allow you to access it from inside the GetCallback object (an inner class) without error. The second way is to refer back to the parent object - if this code is, for example, in an Activity subclass called DisplayTableActivity, you could put a method into it UpdateTable(String updateText, String timeText), and instead of accessing the table directly in the GetCallback object, have the following line:

DisplayTableActivity.this.UpdateTable(updateText, timeText);

That way, you can get the table's views inside that function (which belongs to the Activity) without any problems.

于 2012-06-16T04:52:34.700 回答