我一直在研究用户通过完成挑战获得奖励的应用程序。目前,用户每完成一项挑战就获得10分。但是我需要这样做,以便每次用户完成挑战时都会更新用户分数,换句话说,这些分数将从数据库中提取并添加额外的分数。 (点需要更新,其中userID匹配用户ID)如何检索和更新Firebase中的特定值
我怎么能解决这个问题,谢谢你的任何建议。
public void stopActivity(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure you want to stop?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Get the activity results
String speedText = speedTxt.getText().toString();
String distanceText = distanceTxt.getText().toString();
String timeText = timeTxt.getText().toString();
Toast.makeText(getApplicationContext(),"Your result is: " + "\n" + speedText + "\n" + distanceText + "\n" + timeText, Toast.LENGTH_LONG).show();
//if the result is better
if(Double.parseDouble(speed_challege) == Double.parseDouble(speedText) || Double.parseDouble(speed_challege) < Double.parseDouble(speedText)){
Toast.makeText(getApplicationContext(),"You completed the challenge", Toast.LENGTH_LONG).show();
//award points if challenge is completed
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(score);
}
//if result is worse
else{
Toast.makeText(getApplicationContext(),"Hard Luck Try again", Toast.LENGTH_LONG).show();
}
dialog.dismiss();
}
});
你的leaderbord变量引用什么? –
DatabaseReference mRootRef = FirebaseDatabase.getInstance()。getReference(); DatabaseReference leaderBoard = mRootRef.child(“Leaders”); final DatabaseReference newResult = leaderBoard.push(); – MariusBrave