如何停止在Android中使用array重复我的问题

如何停止在Android中使用array重复我的问题,android,android-studio,Android,Android Studio,我通过数组添加了5个问题,但每次我的问题都会重复。 我想我必须使用循环,但我不知道如何使用循环。 谁能建议我如何使用循环并停止重复我的问题 private Question[] mQuestionBank = new Question[]{ new Question(R.string.question_oceans, true), new Question(R.string.question_mideast, false), new Questio

我通过数组添加了5个问题,但每次我的问题都会重复。 我想我必须使用循环,但我不知道如何使用循环。 谁能建议我如何使用循环并停止重复我的问题

private Question[] mQuestionBank = new Question[]{
        new Question(R.string.question_oceans, true),
        new Question(R.string.question_mideast, false),
        new Question(R.string.question_africa, false),
        new Question(R.string.question_americas, true),
        new Question(R.string.question_asia, true),
};
private int mCurrentIndex = 0;


private void updateQuestion() {
    int question = mQuestionBank[mCurrentIndex].getTextResId();
    mQuestionTextView.setText(question);
}

private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

    if (userPressedTrue == answerIsTrue) {


            righttoast();
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();


    } else {

        wrongtoast();
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        updateQuestion();

    }


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
    {
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);

    }
    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            checkAnswer(true);

        }
    });
    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         checkAnswer(false);
        }
    });

    mNextButton = (Button) findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });
    mpreviousButton = (Button) findViewById(R.id.previous_button);
    mpreviousButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (mCurrentIndex == 0) {
                return;
            }
            mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
            updateQuestion();
        }
    });

}

public void righttoast() {
    LayoutInflater li = getLayoutInflater();
    View layout = li.inflate(R.layout.righttoast, (ViewGroup)       findViewById(R.id.right_toast_layout));
    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM, 0, 0);
    toast.setView(layout);
    toast.show();


}

public void wrongtoast() {
    LayoutInflater li = getLayoutInflater();
    View layout = li.inflate(R.layout.wrongtoast, (ViewGroup) findViewById(R.id.wrong_toast_layout));
    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM, 0, 0);
    toast.setView(layout);
    toast.show();
}


}

将您的
updateQuestion
方法更改为:

private void updateQuestion() {
    if(mCurrentIndex < mQuestionBank.length()){
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mCurrentIndex++;
        mQuestionTextView.setText(question);
    }
}
private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

    if (userPressedTrue == answerIsTrue) {
        righttoast();
        updateQuestion();
    } else {
        wrongtoast();
        updateQuestion();
    }
}

将您的
updateQuestion
方法更改为:

private void updateQuestion() {
    if(mCurrentIndex < mQuestionBank.length()){
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mCurrentIndex++;
        mQuestionTextView.setText(question);
    }
}
private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

    if (userPressedTrue == answerIsTrue) {
        righttoast();
        updateQuestion();
    } else {
        wrongtoast();
        updateQuestion();
    }
}

你的代码对我来说似乎是正确的。你的问题类别是什么?包com.example.puneet.geoquick;/***由Puneet于2016年11月21日创建。*/公共类问题{private int mTextResId;private boolean mAnswerTrue;公共问题(int textresi,boolean answerTrue){mTextResId=textresi;mAnswerTrue=answerTrue;}public int getTextResId(){return mTextResId;}公共布尔值isAnswerTrue(){return mAnswerTrue;}}}}我觉得你的代码是正确的。你的问题类别是什么?包com.example.puneet.geoquick;/***由Puneet于2016年11月21日创建。*/公共类问题{private int mTextResId;private boolean mAnswerTrue;公共问题(int textresi,boolean answerTrue){mTextResId=textresi;mAnswerTrue=answerTrue;}public int getTextResId(){return mTextResId;}公共布尔值isAnswerTrue(){return mAnswerTrue;}}}}我的问题是重复的,你能给你的项目发一个github链接,这样我就可以看@PuneetKhattar吗?我的问题是重复的,你能给你的项目发一个github链接,这样我就可以看@PuneetKhattar吗