我一直在研究用戶通過完成挑戰獲得獎勵的應用程序。目前,用戶每完成一項挑戰就獲得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