【问题标题】:TextView doesn't show Parse.com variablesTextView 不显示 Parse.com 变量
【发布时间】:2015-08-27 10:09:34
【问题描述】:

我有一个 TextView,它假设查看 2 个变量,其中包含 Parse.com 上用户表中的值,一个是 int 值,一个是 String 值。出于某种原因,当我加载活动(它是主要活动)时,它显示:null - 0,当它应该显示时(字符串) - (int)(字符串和int是桌子上的变量。我可以打开另一个活动,但是当我回到 MainActivity 时,它突然显示了应该在 TextView 上的值。我搜索了所有代码,但没有看到原因。

MainActivity.java 查询代码:

ParseQuery<ParseUser> query = new ParseQuery<ParseUser>("_User");
query.getInBackground(userObjId, new GetCallback<ParseUser>() {
    @Override
    public void done(ParseUser user, ParseException e) {
        score = user.getInt("score");
        king = user.getString("king");
    }
}

我也尝试过这种方法来获取值:

score = (int) user.get("score");
king = (String) user.get("king");

请问,我该怎么做才能解决这个问题?我必须在周日之前将应用程序上传到 Play 商店,但我不知道如何修复这个错误......

【问题讨论】:

  • 你应该在getInBackground()done()方法中获取值后在TextView中设置文本。
  • 正如 ved 所说,query.getInBackground() 在后台运行,程序流程不会等到这个过程完成。 done() 是一个回调,让您知道 getInBackground() 已完成其工作,现在您可以使用这些值。还要确保在分配之前进行空检查。如果您可以发布代码,我们可以更好地帮助您。

标签: android parse-platform null textview


【解决方案1】:
onCreate() {
  <..SOME CODE..>

  final TextView tvScore = (TextView)findViewById(R.id.textView_score);
  final TextView tvKing = (TextView)findViewById(R.id.textView_king);
  String userObjId = "<OBJECT_ID_HERE>";
  ParseQuery<ParseUser> query = new ParseQuery<ParseUser>("_User");
  query.getInBackground(userObjId, new GetCallback<ParseUser>() {
      @Override
      public void done(ParseUser user, ParseException e) {
          score = user.getInt("score");
          king = user.getString("king");
          tvScore.setText(String.valueOf(score));
          tvKing.setText(king);
      }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多