I'm currently working on this simple application to create a quiz of ten questions and make it random. This is my progress so far, the questions are random which is what I need, but the problem is the questions are repeating and I only need them to show once. Please help me to fix this bug.
activity_quiz.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginBottom="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:id="@+id/score_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/score"
android:layout_alignParentRight="true"
android:text="0"
android:textSize="20sp"/>
</RelativeLayout>
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:padding="80dp"
android:text="Question"
android:textSize="15sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choice 1"
android:padding="8dp"
android:layout_marginBottom="24dp"
android:id="@+id/choice1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choice 2"
android:padding="8dp"
android:layout_marginBottom="24dp"
android:id="@+id/choice2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choice 3"
android:padding="8dp"
android:layout_marginBottom="24dp"
android:id="@+id/choice3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choice 4"
android:padding="8dp"
android:layout_marginBottom="24dp"
android:id="@+id/choice4" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/backQuiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:text="Back to Table of Content"
/>
</RelativeLayout>
</LinearLayout>```
QuestionsLibrary.java:
public class QuestionsLibrary {
public String mQuestions [] = {
"Moses remains on the peak of Mt. Sinai for 40 days and 40 nights, then returns to the Israelites carrying what?",
"From the burning bush, how does God reply to Moses when Moses asks for his name?",
"Who was appointed as the first hereditary high priest of the Israelites??",
"God and Moses make two new tablets. The third Commandment states that one should remember what?",
"With the help of God and his staff, Moses leads the escape of the Israelites and parts which body of water?",
"What river was Moses sent down by his mother?",
"Where did Moses receive the Ten Commandments from God?",
"How many plagues did God send to Egypt?",
"Who is the Father of the Israelites?",
"Which of the following was NOT one of the 10 plagues on Egypt?"
};
private String mChoices [] [] = {
{"Donkey", "The Ten Commandments", "Food", "Laptop"},
{"Time is Gold", "I think; therefore I am", "I Am that I Am", "Revive me Jett"},
{"Matthew", "Aaron", "Joshua", "Polo"},
{"The Sabbath day", "Valentine Day", "Holy Day", "Judgement Day"},
{"Red Sea", "Pacific Ocean", "Nile lake", "Caribbean Sea"},
{"The Nile", "The Jordan", "The Euphrates", "The Tigris"},
{"Mt. Olympus", "Mt. Diablo", "Mt. Nebo", "Mt. Sinai"},
{"12", "13", "8", "10"},
{"Joshua", "Moses", "Abraham", "Jesus"},
{"Frogs", "Flies", "Darkness", "Floods"}
};
private String mCorrectAnswers [] = {"The Ten Commandments", "I Am that I Am", "Aaron", "The Sabbath day", "Red Sea","The Nile","Mt. Sinai","10","Abraham","Floods"};
public String getQuestion(int a) {
String question = mQuestions[a];
return question;
}
public String getChoice1(int a) {
String choice0 = mChoices[a] [0];
return choice0;
}
public String getChoice2(int a) {
String choice1 = mChoices[a] [1];
return choice1;
}
public String getChoice3(int a) {
String choice2 = mChoices[a] [2];
return choice2;
}
public String getChoice4(int a) {
String choice3 = mChoices[a] [3];
return choice3;
}
public String getCorrectAnswer(int a) {
String answer = mCorrectAnswers[a];
return answer;
}
}
QuizActivity.java:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class QuizActivity extends AppCompatActivity {
TextView mScoreView,mQuestionView;
Button mButtonChoice1,mButtonChoice2,mButtonChoice3,mButtonChoice4,backQuiz;
private QuestionsLibrary mQuestions = new QuestionsLibrary();
private String mAnswer;
private int mScore = 0;
private int num = 0;
private int mQuestionsLenght = mQuestions.mQuestions.length;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_quiz);
r = new Random ();
mScoreView = (TextView) findViewById (R.id.score);
mQuestionView = (TextView) findViewById (R.id.question);
mButtonChoice1 = (Button) findViewById (R.id.choice1);
mButtonChoice2 = (Button) findViewById (R.id.choice2);
mButtonChoice3 = (Button) findViewById (R.id.choice3);
mButtonChoice4 = (Button) findViewById (R.id.choice4);
backQuiz = (Button) findViewById (R.id.backQuiz);
mScoreView.setText ("Score:" + mScore);
updateQuestion (1+r.nextInt(mQuestionsLenght));
mButtonChoice1.setOnClickListener (new View.OnClickListener (){
@Override
public void onClick(View v) {
if (mButtonChoice1.getText () == mAnswer){
mScore = mScore + 1;
mScoreView.setText ("Score:" + mScore);
updateScore(mScore);
updateQuestion (r.nextInt (mQuestionsLenght));
Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
}else {
Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
updateQuestion (r.nextInt (mQuestionsLenght));
}
}
});
mButtonChoice2.setOnClickListener (new View.OnClickListener (){
@Override
public void onClick(View v) {
if (mButtonChoice2.getText () == mAnswer){
mScore = mScore + 1;
mScoreView.setText ("Score:" + mScore);
updateScore(mScore);
updateQuestion (r.nextInt (mQuestionsLenght));
Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
}else {
Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
updateQuestion (r.nextInt (mQuestionsLenght));
}
}
});
mButtonChoice3.setOnClickListener (new View.OnClickListener (){
@Override
public void onClick(View v) {
if (mButtonChoice3.getText () == mAnswer){
mScore = mScore + 1;
mScoreView.setText ("Score:" + mScore);
updateScore(mScore);
updateQuestion (r.nextInt (mQuestionsLenght));
Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
}else {
Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
updateQuestion (r.nextInt (mQuestionsLenght));
}
}
});
mButtonChoice4.setOnClickListener (new View.OnClickListener (){
@Override
public void onClick(View v) {
if (mButtonChoice4.getText () == mAnswer){
mScore = mScore + 1;
mScoreView.setText ("Score:" + mScore);
updateScore(mScore);
updateQuestion (r.nextInt (mQuestionsLenght));
Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show ();
}else {
Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show ();
updateQuestion (r.nextInt (mQuestionsLenght));
}
}
});
backQuiz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(QuizActivity.this, tableofcontentActivity2.class);
startActivity(i);
}
});
}
private void updateQuestion(int num){
mQuestionView.setText (mQuestions.getQuestion (num));
mButtonChoice1.setText (mQuestions.getChoice1 (num));
mButtonChoice2.setText (mQuestions.getChoice2 (num));
mButtonChoice3.setText (mQuestions.getChoice3 (num));
mButtonChoice4.setText (mQuestions.getChoice4 (num));
mAnswer = mQuestions.getCorrectAnswer (num);
}
private void updateScore(int point) {
mScoreView.setText ("" + mScore);
}