Questions is not retrieved properly by CountDownTimer

I want to retrieve questions when one complete the time. It’s like,

Retrieve a question

Wait for 10 seconds

Then retrieve the second question

Wait for 10 seconds

Then the 3rd one …. but the playAnim() method does not the retrieve the questions properly. After finishing first question the 6th question has been come and the app is unfortunately crashed. I share the source code bellow.

myRef.child("SETS")                 .child(setId).addListenerForSingleValueEvent(new ValueEventListener() {             @Override             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {                 for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {                      timmer.setText(String.valueOf(10));                     String id = dataSnapshot1.getKey();                     String question = dataSnapshot1.child("question").getValue().toString();                     String a = dataSnapshot1.child("optionA").getValue().toString();                     String b = dataSnapshot1.child("optionB").getValue().toString();                     String c = dataSnapshot1.child("optionC").getValue().toString();                     String d = dataSnapshot1.child("optionD").getValue().toString();                     String correctANS = dataSnapshot1.child("correctANS").getValue().toString();                      list.add(new QuestionModel(id, question, a, b, c, d, correctANS, setId));                     startTimmer();                 }                   if (list.size() > 0) {                      for (int i = 0; i < 4; i++) {                         optionsContainer.getChildAt(i).setOnClickListener(new View.OnClickListener() {                             @Override                             public void onClick(View v) {                                 checkAnswer((Button) v);                                 countDownTimer.cancel();                             }                         });                     }                                         playAnim(question, 0, list.get(position).getQuestion());   private void startTimmer() {          countDownTimer = new CountDownTimer(10000,1000) {             @Override             public void onTick(long millisUntilFinished) {                 timmer.setText(String.valueOf(millisUntilFinished / 1000));             }              @Override             public void onFinish() {                 changeQuestion() ;             }         };         countDownTimer.start();     }      private void changeQuestion() {         if(list.size()>position){             //position++;            // playAnim();             count = 0;             playAnim(question, 0, list.get(position++).getQuestion());             timmer.setText(String.valueOf(10));             startTimmer();          }             //go to store activity             if (position == list.size()) {                 Intent scoreIntent = new Intent(QuestionActivity.this, ScoreActivity.class);                 scoreIntent.putExtra("score", score);                 scoreIntent.putExtra("total", list.size());                 startActivity(scoreIntent);                 finish();                 return;          }     }    private void playAnim(final View view, final int value, final String data) {           for (int i = 0; i < 4; i++) {             optionsContainer.getChildAt(i).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#989898")));          }         view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500)                 .setStartDelay(100).setInterpolator(new DecelerateInterpolator())                 .setListener(new Animator.AnimatorListener() {                     @Override                     public void onAnimationStart(Animator animation) {                         if (value == 0 && count < 4) {                             String option = "";                             if (count == 0) {                                 option = list.get(position).getA();                             } else if (count == 1) {                                 option = list.get(position).getB();                             } else if (count == 2) {                                 option = list.get(position).getC();                             } else if (count == 3) {                                 option = list.get(position).getD();                             }                             playAnim(optionsContainer.getChildAt(count), 0, option);                             count++;                          }                     }                      @Override                     public void onAnimationEnd(Animator animation) {                         /*                         change data                          */                         if (value == 0) {                              /*                                 if user press inCorrect answer then show the right answer                                */                             try {                                 ((TextView) view).setText(data);                                 noIndicator.setText(position + 1 + "/" + list.size());                                 if (modelMatch()) {                                     bookmarkButton.setImageDrawable(getDrawable(R.drawable.bookmark));                                 } else {                                     bookmarkButton.setImageDrawable(getDrawable(R.drawable.bookmark_borders));                                 }                              } catch (ClassCastException e) {                                 ((Button) view).setText(data);                             }                             // ((TextView)view).setText(data);                             /*                             we use for loop but here we use direct view                              */                             view.setTag(data);                             playAnim(view, 1, data);                         } else {                             enableOption(true);                         }                     }                      @Override                     public void onAnimationCancel(Animator animation) {                      }                      @Override                     public void onAnimationRepeat(Animator animation) {                      }                 });      }     private void checkAnswer(Button selectedOption) {         enableOption(false);         NextButton.setEnabled(true);         NextButton.setAlpha(1);         if (selectedOption.getText().toString().equals(list.get(position).getAnswer())) {             /*             if answer correct then we count the answers              */             score++;             //correct             selectedOption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#4CAF50")));          } else {             //incorrect             selectedOption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ff0000")));             /*             if user press inCorrect answer then show the right answer              */             Button correctoption = (Button) optionsContainer.findViewWithTag(list.get(position).getAnswer());             correctoption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#4CAF50")));           }      }      private void enableOption(boolean enable) {         for (int i = 0; i < 4; i++) {             optionsContainer.getChildAt(i).setEnabled(enable);             /*             for changing the question options              */             if (enable) {                  optionsContainer.getChildAt(i).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#989898")));              }         }     } 
Asked on July 16, 2020 in Firebase,   Java.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.