Java 解析查询:从类中的done()方法返回值?

Java 解析查询:从类中的done()方法返回值?,java,android,parse-platform,Java,Android,Parse Platform,我有以下问题: public int getPoints() { ParseQuery<RoyalPoints> pointsQuery = RoyalPoints.getQuery(); pointsQuery.whereEqualTo("user", ParseUser.getCurrentUser()); pointsQuery.findInBackground(new FindCallback<RoyalPoints>() {

我有以下问题:

public int getPoints() {
    ParseQuery<RoyalPoints> pointsQuery = RoyalPoints.getQuery();
    pointsQuery.whereEqualTo("user", ParseUser.getCurrentUser());
    pointsQuery.findInBackground(new FindCallback<RoyalPoints>() {
        @Override
        public void done(List<RoyalPoints> list, ParseException e) {
            if (e == null) {
                i = 0;
                for (RoyalPoints obj : list) {
                    totalPoints = totalPoints + obj.getInt("points");
                    i = i + 1;
                }
            } else {
                Log.d("Points retrieval", "Error: " + e.getMessage());
            }
        }
    });
    return totalPoints;
}
public int getPoints(){
ParseQuery pointsQuery=RoyalPoints.getQuery();
pointsQuery.whereEqualTo(“user”,ParseUser.getCurrentUser());
findInBackground(新的FindCallback(){
@凌驾
公共作废完成(列表,异常解析){
如果(e==null){
i=0;
对于(对象:列表){
totalPoints=totalPoints+obj.getInt(“点”);
i=i+1;
}
}否则{
Log.d(“点检索”,“错误:+e.getMessage());
}
}
});
返回总分;
}
我想返回totalPoints值,以便在我的MainActivity中使用它。我试着把它放在方法中,改为使用一个void类,但后来我找不到如何调用它。你知道我怎么解决这个问题吗?
谢谢

问题是,当您不想在后台查找时,您正在使用findInBackground方法。请改用“查找”方法:

  public int getPoints() {
      try {
         ParseQuery<RoyalPoints> pointsQuery = RoyalPoints.getQuery();
         pointsQuery.whereEqualTo("user", ParseUser.getCurrentUser());
         List<RoyalPoints> list = pointsQuery.find();
         for (RoyalPoints obj : list) {
            totalPoints = totalPoints + obj.getInt("points");
         }

         return totalPoints;
     } catch (ParseException e) {
         Log.d("Points retrieval", "Error: " + e.getMessage());
     }
  }
public int getPoints(){
试一试{
ParseQuery pointsQuery=RoyalPoints.getQuery();
pointsQuery.whereEqualTo(“user”,ParseUser.getCurrentUser());
List=pointsqury.find();
对于(对象:列表){
totalPoints=totalPoints+obj.getInt(“点”);
}
返回总分;
}捕获(解析异常){
Log.d(“点检索”,“错误:+e.getMessage());
}
}

注意:通常我会通过ParseException或转换为Runtime,因为调用方可能需要对其进行处理。

问题是,当您不想在后台查找时,您正在使用findInBackground方法。请改用“查找”方法:

  public int getPoints() {
      try {
         ParseQuery<RoyalPoints> pointsQuery = RoyalPoints.getQuery();
         pointsQuery.whereEqualTo("user", ParseUser.getCurrentUser());
         List<RoyalPoints> list = pointsQuery.find();
         for (RoyalPoints obj : list) {
            totalPoints = totalPoints + obj.getInt("points");
         }

         return totalPoints;
     } catch (ParseException e) {
         Log.d("Points retrieval", "Error: " + e.getMessage());
     }
  }
public int getPoints(){
试一试{
ParseQuery pointsQuery=RoyalPoints.getQuery();
pointsQuery.whereEqualTo(“user”,ParseUser.getCurrentUser());
List=pointsqury.find();
对于(对象:列表){
totalPoints=totalPoints+obj.getInt(“点”);
}
返回总分;
}捕获(解析异常){
Log.d(“点检索”,“错误:+e.getMessage());
}
}

注意:通常我会通过ParseException或转换为Runtime,因为调用方可能需要对其进行处理。

非常感谢!如何以与if语句不同的方式处理parseException?您可以使用try/catch:try{ParseQuery….}catch{parseException e}{Log.d(“Points retrieval”,“Error:+e.getMessage());}更新原始答案以处理parseException。我想最好还是扔掉这个,因为你可能需要用它做点什么……非常感谢!如何以与if语句不同的方式处理parseException?您可以使用try/catch:try{ParseQuery….}catch{parseException e}{Log.d(“Points retrieval”,“Error:+e.getMessage());}更新原始答案以处理parseException。我认为最好扔掉这个,因为你可能需要用它做点什么。。