从解析中检索数据但在查询外部不可用

时间:2015-03-30 20:58:53

标签: android parse-platform

我想使用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 个答案:

答案 0 :(得分:0)

正如@danh在评论中建议的那样,解析函数(findInBackground等)以异步方式运行。因此,在为firstName和lastName分配查询结果中的任何值之前,textView可以设置这些变量的先前值。如你所说,这些是空的,也许是因为它们没有被分配任何值apriori。

您可以在回调中调用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);  
                } 

或者你可以为ex。

创建一个单独的函数
private void setText(final ParseObject object){
// put your logic here
}

在回调中,

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

希望这有帮助!