Challenge: Add a Listener to the TextView

Hi,

This is my solution for the first Challenge:

    mQuestionTextView = (TextView)findViewById(R.id.question_text_view);

    mQuestionTextView.setOnClickListener(new View.OnClickListener(){
        @Override

        public void onClick(View v){

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

        }

    });
1 Like

Hi!

The same code is used twice: 1) in button ‘Next’, 2) in TextView listener
So I prefer to move it into separate method

private void moveNext() {
    currentIndex = (currentIndex + 1) % questionBank.length;
    updateQuestion();
}

With regards to this line:

`mCurrentIndex=(mCurrentIndex+1)% mQuestionBank.length;`

Why is it necessary to include % mQuestionBank.length;?

1 Like

When we click next next next many times, mCurrentIndex value will be whatever plus 1 such as 21 22 23…
Then modulo(%) by mQuestionBank.lenght (.lenght is static method use to get number of members) the value will be not more than 6 questions that mQuextionBank has.