所以我创建了一个测验游戏,而且我很难从数据库中保存的内容中继续得分。我开始游戏活动并获得30分,例如,如果我退出活动并进入配置文件,在其中找到更新的得分并返回,它将游戏和数据库中的得分重置为0。 我需要能够从上次保存的分数继续。我哪里出问题了? ps。这是我第一次在这里问问题。 这是游戏代码:
package com.sandu.quizz4games;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Random;
public class Game extends AppCompatActivity {
AdView adView;
Button ans1_btn, ans2_btn, ans3_btn, ans4_btn, back_btn;
TextView question, score;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private DatabaseReference myRef;
String userId;
private Questions mQuestions = new Questions();
Random r;
private String mAnswer;
private int mQuestionsLength = mQuestions.mQuestions.length;
int uScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
//Initiate
r = new Random();
adView = findViewById(R.id.add);
ans1_btn = findViewById(R.id.ans1_btn);
ans2_btn = findViewById(R.id.ans2_btn);
ans3_btn = findViewById(R.id.ans3_btn);
ans4_btn = findViewById(R.id.ans4_btn);
back_btn = findViewById(R.id.back_btn);
question = findViewById(R.id.question);
score = findViewById(R.id.score);
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
final FirebaseUser user = mAuth.getCurrentUser();
assert user != null;
userId = user.getUid();
myRef.child("users").child(userId).child("eScore").setValue(uScore);
//...//
// update question randomizer
updateQuestion(r.nextInt(mQuestionsLength));
//...//
//Button Functions
ans1_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans1_btn.getText() == mAnswer){
uScore+=10;
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
ans2_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans2_btn.getText() == mAnswer){
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
uScore+=10;
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
ans3_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans3_btn.getText() == mAnswer){
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
uScore+=10;
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
ans4_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans4_btn.getText() == mAnswer){
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
uScore+=10;
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Game.this,Home.class);
startActivity(i);
}
});
//...//
// Full Screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//.....//
// AdMob add - Banner
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
//...//
}
// Update Question Function
private void updateQuestion(int num){
question.setText(mQuestions.getQuestions(num));
ans1_btn.setText(mQuestions.getChoice1(num));
ans2_btn.setText(mQuestions.getChoice2(num));
ans3_btn.setText(mQuestions.getChoice3(num));
ans4_btn.setText(mQuestions.getChoice4(num));
mAnswer = mQuestions.getCorrectAnswer(num);
}
//...//
//Other Functions
private void gameOver(){
uScore -=5;
Toast.makeText(Game.this,"Wrong", Toast.LENGTH_SHORT).show();
if (uScore < 0){
uScore = 0;
score.setText("0");
}
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
updateQuestion(r.nextInt(mQuestionsLength));
}
//...//
}
答案 0 :(得分:0)
如果您的数据库中存在myRef.child("users").child(userId).child("eScore")
,请先从数据库中读取其得分并将其保存到uScore
变量中。