0
我做了一個查詢以從我的Firebase中檢索特定數據。在這種情況下,它的用戶得分。我如何更新剛剛檢索的相同值?如何更新Firebase中的數據
感謝所有幫助
//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) {
}
});