如何使用旧值

时间:2017-12-21 21:28:49

标签: android database firebase firebase-realtime-database

如何更新数据库中的分数,我想在数据库中添加玩家获得的分数,但是没有找到如何执行该分数

this is my firebase image

这里是我每次用户结束游戏时应用的代码,数据库中的分数更新但不知道如何用较旧的分数更新分数

Button btnTryAgain;
TextView txtResultScore,getTxtResultQuestion;
RingProgressBar ringProgressBar;




FirebaseAuth mAuth;
FirebaseDatabase database;
DatabaseReference questions_score;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_done);
    mAuth = FirebaseAuth.getInstance();
    database = FirebaseDatabase.getInstance();
    questions_score = database.getReference("Question_Score");



    txtResultScore = (TextView)findViewById(R.id.txtTotalScore);
    getTxtResultQuestion  = (TextView) findViewById(R.id.txtTotalQuestion);
    ringProgressBar = (RingProgressBar) findViewById(R.id.progressbarRingDone);
    btnTryAgain = (Button) findViewById(R.id.btnTryAgain);

    btnTryAgain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent (Done.this,Home.class);
            startActivity(intent);
            finish();

        }
    });


    //get data from bundle and set to view
    Bundle extra = getIntent().getExtras();
    if (extra != null) {
        int score = extra.getInt("SCORE");
        int totalQuestion = extra.getInt("TOTAL");
        int correctAnswer = extra.getInt("CORRECT");


        txtResultScore.setText(String.format("SCORE : %d", score));
        getTxtResultQuestion.setText(String.format("PASSED : %d / %d", correctAnswer, totalQuestion));


        ringProgressBar.setMax(totalQuestion);
        ringProgressBar.setProgress(correctAnswer);


        //upload points to DB
        questions_score.child(String.format("%s_%s", Common.currentUser.getUid(),
                Common.categoryId))
                .setValue(new QuestionScore(String.format("%s_%s", Common.currentUser.getDisplayName(),
                        Common.categoryId),
                        Common.currentUser.getDisplayName(),
                        String.valueOf(score),
                        Common.currentUser.getEmail(),
                        Common.currentUser.getPhotoUrl().toString(),
                        Common.categoryId,
                        Common.categoryName,
                        Common.currentUser.getUid()));


    }
}

}

0 个答案:

没有答案