如何更新firebase中的数据

时间:2017-04-08 12:27:44

标签: android firebase firebase-realtime-database

我已经进行了查询以从我的firebase中检索特定数据。在这种情况下,其用户得分。如何更新我刚检索到的相同值?

感谢您的帮助

enter image description here

//check the leaders and their scores, get the score of the current user
    DatabaseReference leadersRef = FirebaseDatabase.getInstance().getReference("Leaders");
    Query query = leadersRef.orderByChild("uID").equalTo(mCurrentUser.getUid());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if(snapshot.exists()){
                    for (DataSnapshot child: snapshot.getChildren()) {
                        currentScore = child.child("Score").getValue(Integer.class);
                        Toast.makeText(RunningChallengeActivity.this,"Your current score: " + currentScore,Toast.LENGTH_LONG).show();
                    }
                }
                else {

                //if user has no points create new leader child and set score to 0
                final DatabaseReference newResult = leaderBoard.push();
                String username = mCurrentUser.getEmail();

                //trim email leaving just username
                int index = username.indexOf('@');
                username = username.substring(0,index);

                newResult.child("UserName").setValue(username);
                newResult.child("uID").setValue(mAuth.getCurrentUser().getUid());
                newResult.child("Score").setValue(0);

                }
            }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

2 个答案:

答案 0 :(得分:1)

这样的事情:

DatabaseReference leadersRef = FirebaseDatabase.getInstance().getReference("Leaders");
Query query = leadersRef.orderByChild("uID").equalTo(mCurrentUser.getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot child: snapshot.getChildren()) {
            currentScore = child.child("Score").getValue(Integer.class);
            child.getRef().child("Score").setValue(42);
        }
    }

如果您想根据当前值设置分数的新值,您需要改为use a database transaction

答案 1 :(得分:1)

//check the leaders and their scores, get the score of the current user
    final DatabaseReference leadersRef = FirebaseDatabase.getInstance().getReference("Leaders");
    final Query query = leadersRef.orderByChild("uID").equalTo(mCurrentUser.getUid());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if(snapshot.exists()){
                for (DataSnapshot child: snapshot.getChildren()) {

                    //get the key of the child node that has to be updated
                    String postkey = child.getRef().getKey();

                    //update score
                    int new_score = currentScore + score;
                    leadersRef.child(postkey).child("Score").setValue(new_score);
                    Toast.makeText(RunningChallengeActivity.this,"Your current score: " + new_score +"",Toast.LENGTH_LONG).show();

                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });