如何在 Parse 中添加列的值?

How to add value of column in Parse?

ParseObject money = new ParseObject("Money");
money.put("value", value);
money.put("description", description);

money.saveInBackground();

我正在开发 Android 的应用程序,它是一种计算器。我添加数据来解析我赚了多少钱和花了多少钱,所以每一行都有价值(多少钱)和描述。现在我需要将列 "value" 中的所有值相加并显示在另一个 Activity 中。

我该怎么做?

Parse 不支持通过查询进行聚合。您需要使用 CloudCode 来执行此操作,请查看此示例:https://parse.com/docs/cloud_code_guide#functions

以下是如何计算总价值的示例:

private int getTotal() {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Money");
    try {
        int total = 0;
        List<ParseObject> objects = query.find();
        for (ParseObject p : objects) {
            total += p.getInt("value");
        }
        return total;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}