1

I am a teacher and creating a quiz for my students rest is complete I have 500 questions on my firebase real-time database all I want to do is randomize questions with no repetation each time 50 questions and set a listener after 50 questions to show results

Here my quiz activity

public class grandtest extends AppCompatActivity {

TextView mQuestionTextView;
ProgressBar mProgressBar;
Button bchoice1,bchoice2,bchoice3,bchoice4;
Random Rand;
String mAnswer;
Timer mTimer;
TextView mScoreTextView;
public static int mScore;
int mQuestionNo = 0;

private Firebase mQuestionRef;
private Firebase mChoice1Ref;
private Firebase mChoice2Ref;
private Firebase mChoice3Ref;
private Firebase mChoice4Ref;
private Firebase mAnswerRef;

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

    mQuestionTextView = findViewById(R.id.mquestiontextview);
    mProgressBar = findViewById(R.id.progressBar);
    bchoice1 = findViewById(R.id.choice1);
    bchoice2 = findViewById(R.id.choice2);
    bchoice3 = findViewById(R.id.choice3);
    bchoice4 = findViewById(R.id.choice4);
    mScoreTextView = findViewById(R.id.mScore);
}


public void updateQuestion (){

    mQuestionRef = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo +"/question");
    mQuestionRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
        String question = dataSnapshot.getValue(String.class);
        mQuestionTextView.setText(question);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Toast.makeText(grandtest.this,"Please enable data",Toast.LENGTH_SHORT).show();
        }
    });
    mChoice1Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice1");
    mChoice1Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice1 = dataSnapshot.getValue(String.class);
            bchoice1.setText(choice1);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice2Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice2");
    mChoice2Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice2 = dataSnapshot.getValue(String.class);
            bchoice2.setText(choice2);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice3Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice3");
    mChoice3Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice3 = dataSnapshot.getValue(String.class);
            bchoice3.setText(choice3);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice4Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice4");
    mChoice4Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice4 = dataSnapshot.getValue(String.class);
            bchoice4.setText(choice4);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mAnswerRef = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/answer");
    mAnswerRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mAnswer = (String) dataSnapshot.getValue(String.class);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mQuestionNo ++;

I want to randomize mQuestionNo object to retrieve random questions each time. Here is the structure of my firebase data

Firebase Data

Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
Tanveer Ahmed
  • 426
  • 1
  • 4
  • 15
  • It would probably be a good idea to create a Class to handle your objects. Instead of calling 6 firebase references you can call one, and then convert the data you get back into a custom object which can hold the values. – Martin Lund Oct 29 '18 at 11:11

3 Answers3

1

This will not solve your problem, but help you on a path to better coding! :)

Create a class to contain your question

public class Question {

    String question;
    String choice1;
    String choice2;
    String choice3;
    String choice4;
    String answer;
}

Then in your activity you can call just one databaseReference instead of multiple.

    int questionNo = 0;

    mDatabase.child(String.valueOf(questionNo)).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Question question = new Question();
            question.answer = dataSnapshot.child("answer").getValue().toString();
            question.choice1 = dataSnapshot.child("choice1").getValue().toString();
            question.choice2 = dataSnapshot.child("choice2").getValue().toString();
            question.choice3 = dataSnapshot.child("choice3").getValue().toString();
            question.choice4 = dataSnapshot.child("choice4").getValue().toString();
            question.question = dataSnapshot.child("question").getValue().toString();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

Here we fetch question at 0 index.

You can get 50 random keys like this:

    ArrayList<Integer> questions = new ArrayList<>();

    while(questions.size() < 50){
        int random = new Random().nextInt(500 + 1)  + 1;
        if (!questions.contains(random)){
            questions.add(random);
            //Question added
        }else{
            //Question already added to list
        }
    }

Now you have the keys for the questions you want to traverse. You can then call questions.get(49) to get the last question in all of your keys, check your firebase with this index, and when firebase has gotten the question, you can remove this index with question questions.remove(49); and go to next index. Of course the 49 should not be hardcoded but a value you set as int questionCount = 49; and then when you have done all of this you would --questionCount;

This is a bit psuedo right now, but hopefully it can give you some ideas :)

Martin Lund
  • 1,159
  • 9
  • 17
0
  • Create an ArrayList containing key values of all questions
  • Then select a random key from the arraylist and after selecting the data from firebase and then remove the key from the arrayList, the increment the counter by and repeat this step until counter reaches 50
  • After counter reaches 50 show the results
Jinson Paul
  • 481
  • 6
  • 17
0

You can create a Set for storing the previous questions and by using the following method you can retrieve the next question number.

Set<Integer> set = new HashSet<>();

public int getNextQuestionNumber() {
    int num = (int) (Math.random() * 500); //Total number of questions in the database.
    if (set.contains(num)) {
        return getNextQuestionNumber();
    } else {
        set.add(num);
        return num;
    }
}
Bhupendra Joshi
  • 233
  • 1
  • 5
  • 8