我遇到以下问题:
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方法。使用查找方法:
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或转换到Runtime,因为调用者可能需要对它做些什么。