0

我想将每个点击按钮的当前用户附加到类型列表的列中,以便稍后我可以得到有多少人按下它的长度。

这是我的代码:

public void findAnswered(CloudQuery query) throws CloudException {
    query.findById(questionID, new CloudObjectCallback() {
        @Override
        public void done(CloudObject object, CloudException e) throws CloudException {
            if (object != null) {
                Log.i("---------FIND ANS QUERY", "SUCCESS I: " + object.getID());
                answered(object);
            }
            if (e != null) {
                Log.i("---------FIND ANS QUERY", "ERROR: " + e.getLocalizedMessage());
            }
        }
    });
}

public void answered(CloudObject object) throws CloudException {
    CloudObject [] user = {CloudUser.getcurrentUser()};

    Log.i("------UPDATE ANS OBJECT", "A: " + this.answered);
    Log.i("------UPDATE ANS OBJECT", "W: " + this.wrongAnswer);
    Log.i("------UPDATE ANS OBJECT", "R: " + this.rightAnswer);

    object.set("earnings", earnings);
    object.set("dividend", dividend);
    object.set("answered", user);
    if (this.wrongAnswer == 1) {
        object.set("wrong", user);
    }
    if (this.rightAnswer == 1) {
        object.set("right", user);
    }

    object.save(new CloudObjectCallback() {
        @Override
        public void done(CloudObject object, CloudException e) throws CloudException {
            if (object != null) {
                // Success
                Log.i("------UPDATE ANS OBJECT", "SUCCESS I: " + object.getId());
            }
            if (e != null) {
                // Error
                Log.i("------UPDATE ANS ERROR:", e.getLocalizedMessage());
            }
        }
    });
}

但是当我尝试在两部手机上执行此操作时,它只会添加第一个单击它的用户并跳过第二个用户。如何继续将用户添加到同一个框?

4

1 回答 1

0

I think the problem is on this line
CloudObject [] user = {CloudUser.getcurrentUser()};
You are creating an array with only the current user, so that when you update the CloudObject with object.set("answered", user); , you are simply overwriting any existing List of Users in your list column and not really appending current user to existing users as you actually want.
What you should be doing is to retrieve all existing users into an appropriate data structure (like an ArrayList or a Set), and append the current user with users.add(CloudUser.getCurrentUser()) before updating your CloudObject

于 2016-03-29T06:17:40.973 回答