Я не показывал данные в макете, но в logcat показывается вывод. Я храню некоторые данные в базе данных реального времени firebase, и это работает, но эти извлеченные данные не могут отображаться в макете.
ShowQuiz Activity:
public class ShowQuizActivity extends AppCompatActivity implements View.OnClickListener {
final static int INTERVAL = 1000;
final static int TIMEOUT = 7000;
int progressValue = 0;
List<Question> questionList;
CountDownTimer mCountDownTimer;
ProgressBar progressBar;
Button btnA,btnB,btnC,btnD;
TextView txtScore,txtQuestionNum,txtQuestion;
int score = 0,index = 0,thisQuestion = 0,totalQuestion,correctAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_quiz);
questionList = new ArrayList<>();
progressBar = findViewById(R.id.progressBar);
txtQuestion = findViewById(R.id.question);
txtQuestionNum = findViewById(R.id.quNum);
txtScore = findViewById(R.id.score);
btnA = findViewById(R.id.option1);
btnB = findViewById(R.id.option2);
btnC = findViewById(R.id.option3);
btnD = findViewById(R.id.option4);
btnA.setOnClickListener(this);
btnB.setOnClickListener(this);
btnC.setOnClickListener(this);
btnD.setOnClickListener(this);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference questionRef = rootRef.child("Question");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//List<Question> questionList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Question q = ds.getValue(Question.class);
questionList.add(q);
String answer = q.getAnswer();
String option1 = q.getOption1();
String option2 = q.getOption2();
String option3 = q.getOption3();
String option4 = q.getOption3();
String question = q.getQuestion();
Log.d("=>", answer + "/" + option1 + "/" + option2 + "/" + option3 + "/" + option4 + "/" + question);
}
//Do what you need to do with your list
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("=>", databaseError.getMessage()); //Don't ignore errors!
}
};
questionRef.addListenerForSingleValueEvent(valueEventListener);
}
@Override
public void onClick(View view) {
mCountDownTimer.cancel();
if (index < totalQuestion){
Button ClickedButton = (Button) view;
if (ClickedButton.getText().equals(questionList.get(index).getAnswer())){
score+=10;
correctAnswer++;
showQuestion(index++);
}
else {
showQuestion(++index);
}
txtScore.setText(String.format("%d",score));
}
}
private void showQuestion(int index) {
if (index < totalQuestion){
thisQuestion++;
txtQuestionNum.setText(String.format("%d/%d",thisQuestion,totalQuestion));
progressBar.setProgress(0);
progressValue = 0;
txtQuestion.setText(questionList.get(index).getQuestion());
btnA.setText(questionList.get(index).getOption1());
btnB.setText(questionList.get(index).getOption2());
btnC.setText(questionList.get(index).getOption3());
btnD.setText(questionList.get(index).getOption4());
}
else {
Intent intent = new Intent(this,ScoreActivity.class);
Bundle dataSend = new Bundle();
dataSend.putInt("SCORE",score);
dataSend.putInt("TOTAL",totalQuestion);
dataSend.putInt("CORRECT",correctAnswer);
intent.putExtras(dataSend);
startActivity(intent);
finish();
}
}
@Override
protected void onResume() {
super.onResume();
totalQuestion = questionList.size();
mCountDownTimer = new CountDownTimer(TIMEOUT,INTERVAL) {
@Override
public void onTick(long l) {
progressBar.setProgress(progressValue);
progressValue++;
}
@Override
public void onFinish() {
mCountDownTimer.cancel();
showQuestion(++index);
}
};
showQuestion(++index);
}
}
Question class is here:
public class Question {
private String answer,option1,option2,option3,option4,question;
public Question() {
}
public Question(String answer, String option1, String option2, String option3, String option4, String question) {
this.answer = answer;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption3() {
return option3;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public String getOption4() {
return option4;
}
public void setOption4(String option4) {
this.option4 = option4;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
Источник выходного изображения Logcat: https://i.pinimg.com/originals/48/d4/5d/48d45de210192fa3922c24151e3b21de.png Что я могу сделать? Пожалуйста, помогите мне. Спасибо