首页 > 解决方案 > 方法跳过以执行一些代码

问题描述

代码

 private void updateQuestion() {
    mDatabaseReference.child("Users").child(RecieversId).child("Quiz").child("Question" + mQuestionNumber).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);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mQuestionNumber++;
                        qn.setText("Question : " + mQuestionNumber);
                        updateQuestion();
                        opt1.setBackgroundColor(Color.CYAN);
                    }
                }, 1500);
            } 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);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mQuestionNumber++;
                        qn.setText("Question : " + mQuestionNumber);
                        updateQuestion();
                        opt2.setBackgroundColor(Color.CYAN);
                    }
                }, 1500);
            } 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++;
                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);
            } 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);
                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
                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);
        }
    });
    }
}

它的行为很奇怪。当我得到第一个问题并且我选择了错误的答案时,它会毫无问题地进入下一个问题。但是,当我选择正确答案时,它会跳过下一个问题并转到之后的问题。例如,如果我在问题 2 中选择正确答案,它会跳过问题 3 并显示问题 4。这很奇怪,我无法弄清楚我哪里出错了。

我确定我的 if 语句中有一些错误,但无法弄清楚它是什么。

标签: javaandroidfirebasefirebase-realtime-database

解决方案


在您的两个处理程序中,您正在调用

 mQuestionNumber++;
 updateQuestion();

如果答案正确,则呼叫两次

if(correct){
   handler //once
}else{
}
handler //twice

解决方案是删除first //once处理程序

IE

final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mQuestionNumber++;
                        qn.setText("Question : " + mQuestionNumber);
                        updateQuestion();
                        opt1.setBackgroundColor(Color.CYAN);
                    }
                }, 1500);

if condition

更新为opt1

opt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (option1.equals(answer)) {
                opt1.setBackgroundColor(Color.GREEN);
                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);
        }
    });

推荐阅读