我遇到以下问题:
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 或转换为运行时,因为调用者可能需要用它做一些事情。
我是一名优秀的程序员,十分优秀!