Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.taking_quiz);
que = (TextView) findViewById(R.id.txtQuestion);
opt1 = (Button) findViewById(R.id.button1);
opt2 = (Button) findViewById(R.id.button2);
opt3 = (Button) findViewById(R.id.button3);
opt4 = (Button) findViewById(R.id.button4);
qn = (TextView) findViewById(R.id.number);
sco = (TextView) findViewById(R.id.score);
qn.setText("1");
sco.setText("Score : 0");
qn.setText("Question : 1");
RecieversId = getIntent().getStringExtra("Recievers_Id");
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(RecieversId).child("Quiz").child("Question" + mQuestionNumber);
updateQuestion();
}
private void updateQuestion() {
if (mDatabaseReference != null) {
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.child("Question").getValue().toString();
answer = dataSnapshot.child("Answer").getValue().toString();
option1 = dataSnapshot.child("Option1").getValue().toString();
option2 = dataSnapshot.child("Option2").getValue().toString();
option3 = dataSnapshot.child("Option3").getValue().toString();
option4 = dataSnapshot.child("Option4").getValue().toString();
que.setText(question);
opt1.setText(option1);
opt2.setText(option2);
opt3.setText(option3);
opt4.setText(option4);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
opt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option1.equals(answer)) {
opt1.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
} else
opt1.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option2.equals(answer)) {
opt2.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
} else
opt2.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt2.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option3.equals(answer)) {
opt3.setBackgroundColor(Color.GREEN);
mScore++;
} else
opt3.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt3.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option4.equals(answer)) {
opt4.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
} else
opt4.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt4.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
} else {
Intent intent = new Intent(TakingQuiz.this, TakingQuizDone.class);
intent.putExtra("Score", mScore);
startActivity(intent);
}
}
I'm trying to make a quiz app in Android using a firebase database.
When you click on one of the buttons (answer the question) the question number will increase and according to that question will also increase. But the ++ is working and the TextView
which shows question number is showing that the question number is increasing but the question is not refreshing.
Like in the database I'm getting first question itself and the same options.
Here I guess the updateQuestion()
method is not being called after buttonclick. please help
Database - https://ibb.co/emVEVo
It seems your firebase database reference (mDatabaseReference)
is null so first of all check this. if it is not null
then use debug log to check which line of code is not working
.
好的,我发现了什么问题...我已经在onCreate而不是方法内部初始化了数据库引用...对不起,麻烦
Your Firebase Database Reference is getting the wrong tree.
In your image have more more fathers before " Quiz ", there's VwtdCRK... before and other...
When you use FirebaseDatabase.getInstance().getReference()
it takes the root of your database. Should be like:
FirebaseDatabase.getInstance().getReference().child("rootNotShowingInimage")
.child("VwtdCRK0AXQ2bnKe01fy8mYUPqs2").child("Quiz").child("Question"...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.