2012-06-16 20 views
0

因此,我現在使用Parse來運行我的應用程序的後端。當您查詢您解析數據庫,數據在一個看似不可思議的方式返回(給我,但我是新來的這個) -如何使用getCallback之外的getCallback提供的數據

  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,表,我這裏是沒有相關的代碼片段 -

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

我不是因此目前無法測試,但這樣的事情應該工作:

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,所以一切都應該工作。

+0

似乎不起作用,因爲循環增量符'i'在else語句中不可用。我一直試圖選擇基於文本的行(這將匹配返回對象中的一個值)無濟於事。任何其他想法? – gregamariani

+0

@ user1434163對不起,這是我的一個疏忽。我已經更新了我的答案,因此現在應該可以使用。 –

+0

真棒...男人,我嘗試了很多不同的東西,但由於某種原因,這不是其中之一。你是一個拯救生命的人,我的項目現在可以前進!謝謝。 – gregamariani

0

有兩種可能的解決方法。正如Darshan所說,其中一個就是最後決定桌子。這將允許您從GetCallback對象(內部類)中訪問它,而不會出錯。第二種方法是引用父對象 - 例如,如果此代碼位於名爲DisplayTableActivity的Activity子類中,則可以將方法放入其中UpdateTable(String updateText, String timeText),而不是直接在GetCallback對象中訪問表,下面一行:

DisplayTableActivity.this.UpdateTable(updateText, timeText); 

這樣一來,就可以得到該函數內部表的意見(屬於活動)沒有任何問題。

+0

問題是,我需要讓UpdateTable()函數(使用您的示例)根據for循環中的增量器更新哪一行。我將它想象成在每個循環中,它將查詢一行並用返回的數據更新該行。也許我在想它錯了嗎? – gregamariani

相關問題