为什么我一次只调用一次 TextViews 中的文本会更改两次?

Why is the text in TextViews changing two times when I am calling it only once at a time?

我知道 setText 只更改了一次文本,但在我继续 quizActivity[ 中的下一个问题之前,我似乎无法找到其中文本更改的原因=20=]

我做的是一个应用程序,里面有一个 activity,里面有一个测验,一个问题和 4 个选项一起显示。当用户选择一个选项时,如果该选项正确,则它变为绿色和红色,否则,我会打开一个对话框,显示答案是对还是错,然后当用户单击 Next 在对话框上。

但是,当用户选择一个选项时,在单击该选项然后单击对话框中的下一步的过程中,问题和选项中的文本发生了变化,我似乎无法弄清楚为什么会这样。总共,问题和选项改变了两次,而它们应该只改变一次,意外的改变是当用户点击一个选项并打开对话框时。

代码如下:

@Override
    public void onClick(View view) {

        int selectedOption = 0;

        switch (view.getId()) {

            case R.id.option_1_tile:
                selectedOption = 1;
                break;
            case R.id.option_2_tile:
                selectedOption = 2;
                break;
            case R.id.option_3_tile:
                selectedOption = 3;
                break;
            case R.id.option_4_tile:
                selectedOption = 4;
                break;

            default:

        }

        checkAnswer(selectedOption, view);
    }

这是检查答案的函数:

private void checkAnswer(int selectedOption, View view) {

        if (selectedOption == selected_questions.get(quesNum).getAnswer()) {
            //Right Answer
            (view).setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
            quizReference.child(selected_questions.get(quesNum).getId()).child("correct_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getCorrect_attempts()) + 1));
            quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));
            score++;
            correctDialog();

        } else {
            //Wrong Answer
            (view).setBackgroundTintList(ColorStateList.valueOf(Color.RED));

            quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));

            switch (selected_questions.get(quesNum).getAnswer()) {
                case 1:
                    options[0].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
                    break;
                case 2:
                    options[1].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
                    break;
                case 3:
                    options[2].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
                    break;
                case 4:
                    options[3].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
                    break;

            }

            wrongDialog ();

        }


    }

这是改变问题的函数:

 private void changeQuestion() {
    
            resetColor ();
    
            if (quesNum < selected_questions.size() - 1) {
    
                quesNum++;
    
                playAnim(question, 0, 0);
                playAnim(option1_text, 0, 1);
                playAnim(option2_text, 0, 2);
                playAnim(option3_text, 0, 3);
                playAnim(option4_text, 0, 4);
    
                qCount.setText(String.valueOf(quesNum + 1) + "/" + String.valueOf(selected_questions.size()));
    
    
            } else {
                // Go to Score Activity
                Intent intent = new Intent(quizActivity.this, scoreActivity.class);
                intent.putExtra("SCORE", String.valueOf(score) + "/" + String.valueOf(selected_questions.size()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
    
            }
    
    
        }

这是设置文本和动画的函数:

private void playAnim(final View view, final int value, final int viewNum) {
        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) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (value == 0) {
                            switch (viewNum) {
                                case 0:
                                    ((TextView) view).setText(selected_questions.get(quesNum).getQuestion());
                                    break;
                                case 1:
                                    ((TextView) view).setText(selected_questions.get(quesNum).getOption1());
                                    break;
                                case 2:
                                    ((TextView) view).setText(selected_questions.get(quesNum).getOption2());
                                    break;
                                case 3:
                                    ((TextView) view).setText(selected_questions.get(quesNum).getOption3());
                                    break;
                                case 4:
                                    ((TextView) view).setText(selected_questions.get(quesNum).getOption4());
                                    break;

                            }


                            if (viewNum != 0)
                                (view).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#E99C03")));


                            playAnim(view, 1, viewNum);

                        }

                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });

    }

这是对话框的代码:

 public void wrongDialog() {
        final Dialog dialogWrong = new Dialog(quizActivity.this);
        dialogWrong.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (dialogWrong.getWindow() != null) {
            ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
            dialogWrong.getWindow().setBackgroundDrawable(colorDrawable);
        }
        dialogWrong.setContentView(R.layout.dialog_wrong);
        dialogWrong.setCancelable(false);
        dialogWrong.show();



        TextView wrongText = (TextView) dialogWrong.findViewById(R.id.wrongText);
        Button buttonNext = (Button) dialogWrong.findViewById(R.id.dialogNext);

        //OnCLick listener to go next que
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //This will dismiss the dialog
                dialogWrong.dismiss();

                //reset the color of buttons back to white
                resetColor();

                //Change question
                changeQuestion();


            }
        });
    }

    public void correctDialog() {
        final Dialog dialogCorrect = new Dialog(quizActivity.this);
        dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (dialogCorrect.getWindow() != null) {
            ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
            dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
        }
        dialogCorrect.setContentView(R.layout.dialog_correct);
        dialogCorrect.setCancelable(false);
        dialogCorrect.show();

     


        TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
        Button buttonNext = (Button) dialogCorrect.findViewById(R.id.dialogNext);

        //OnCLick listener to go next que
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //This will dismiss the dialog
                dialogCorrect.dismiss();

                //reset the color of buttons back to white
                resetColor();

                //it will increment the question number
                changeQuestion();


            }
        });
    }

尽管我很乐意回答您可能想要的任何其他 information/code,但我已尽力解释它。此外,如果您有时间 运行 并更好地理解问题,这个 is the link 项目。

我已经检查了你的代码。您在 setUpdates 方法中放置了一个 addValueEventListener。当您 select 一个选项时,您可以通过设置总尝试次数等字段来更新 firestore 数据库。结果,触发了 eventListener 并调用了“selectQuestionSet”函数。

因此,每次 select 一个选项时,都会调用 selectQuestionSet 函数。你应该确保它在开始时只调用一次。