gpt4 book ai didi

java - 解析查询: return a value from the done() method in the class?

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:38 25 4
gpt4 key购买 nike

我遇到以下问题:

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;
}

我想返回值totalPoints以在我的MainActivity中使用它。我尝试将其放入方法中并改为使用 void 类,但之后我找不到如何调用它。知道我该如何解决这个问题吗?谢谢

最佳答案

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

  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());
}
}

注意:通常我会通过 ParseException 或转换为运行时,因为调用者可能需要用它做一些事情。

关于java - 解析查询: return a value from the done() method in the class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com