Posting the whole code in my main activity. Please help me how can a show my last question also when I click next button.
package com.example.quiznew;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button mTrueButton; //inflated widget
private Button mFalseButton; //from layout
private Button mNextButton;
private Button mPreviousButton;
private TextView mQuestionTextView;
private TrueFalse mQuestionBank = new TrueFalse{
new TrueFalse(R.string.qus_1, true),
new TrueFalse(R.string.qus_2, false),
new TrueFalse(R.string.qus_3,true),
new TrueFalse(R.string.qus_4,false),
};
private int mCurrentIndex = 0;
private int totalQuestion = mQuestionBank.length - 1;
private void updateQuestion(){
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
int msgResId = 0;
if (userPressedTrue == answerIsTrue){
msgResId = R.string.correct_toast;
} else {
msgResId = R.string.incorrect_toast;
}
Toast.makeText(this, msgResId, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestionTextView = (TextView)findViewById(R.id.qus_view);
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % totalQuestion;
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex - 1) % totalQuestion;
if (mCurrentIndex < 0){
mCurrentIndex = totalQuestion;
}
else {
mCurrentIndex = mCurrentIndex;
}
updateQuestion();
}
});
mTrueButton = (Button)findViewById(R.id.true_button); //calling button by id
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mQuestionTextView = (TextView)findViewById(R.id.qus_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % totalQuestion;
updateQuestion();
}
});
}
}