2015-03-30 28 views
0

我要使用的數據我從getInBackground()功能外解析得到但字段的值之外是null檢索從解析數據,但不提供查詢

ParseQuery<ParseObject> query = ParseQuery.getQuery("Person"); 
      query.getInBackground(invitedBy.getObjectId(), new GetCallback<ParseObject>() { 
       public void done(ParseObject object, ParseException e) { 
        if (e == null) { 
         // object will be your game score 
//here the variables have an actual value 
         firstName = object.getString("firstName").toString(); 
         lastName = object.getString("lastName").toString(); 
        } else { 
         // something went wrong 
        } 
       } 
      }); 
    //here the values for the firstName and lastName are null 
     invitorName.setText(firstName + " " + lastName); 

任何建議如何解決這個問題。謝謝

+1

大多數解析函數是異步運行的。這意味着他們開始了,並且您的應用程序可以同時進行。在發佈的代碼中,當執行達到setText時,getInBackground甚至還沒有開始。但getInBackground在完成時需要執行一個回調參數。在其中執行setText。 – danh 2015-03-30 22:30:44

回答

0

由於@danh在評論中建議,解析函數(findInBackground等)異步運行。因此,在firstName和lastName從查詢結果中分配任何值之前,textView可以設置這些變量的先前值。如你所說,這些可能是空的,也許是因爲它們沒有被賦予任何先驗值。

您可以在回調中調用invitorName.setText(firstName + " " + lastName);

if (e == null) { 
         // object will be your game score 
//here the variables have an actual value 
         firstName = object.getString("firstName").toString(); 
         lastName = object.getString("lastName").toString(); 
         invitorName.setText(firstName + " " + lastName); 
       } 

或者你可以創建一個單獨的函數說爲前。

private void setText(final ParseObject object){ 
// put your logic here 
} 
回調

if (e == null) { 
     setText(object);      
} 

希望這有助於!