1

I am trying to make a quiz app using Firebase but I need to get 5 questions in total from different topics randomly and add them to ArrayList<object>. I already have generated 1 random question from only 1 topic (child) and I can not make this for the other 4 topics to have 5 questions from different topics My Firebase database is here: enter image description here

My code is:

final Query questionFromB = FirebaseDatabase.getInstance().getReference().child("B").orderByChild("questionID").equalTo(new Random().nextInt(15));
questionFromB.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot datasnapshot) {
        for (DataSnapshot question : datasnapshot.getChildren()){
            if (questionCount < 1){
                questionView.setText(question.getValue(QuizQuestions.class).getQuestionText());
                answerText.setText(question.getValue(QuizQuestions.class).getAnswerText());

                arrayList.add(new QuizQuestions(question.getValue(QuizQuestions.class).getQuestionID(),question.getValue(QuizQuestions.class).getQuestionText(),question.getValue(QuizQuestions.class).getAnswerText()));
                datasnapshot.getChildrenCount();
                questionCount ++;
            }else{
                nextButton.setVisibility(View.GONE);
                nextActivityButton.setVisibility(View.VISIBLE);
                for (int i=0;i<arrayList.size();i++){
                    textView.append(arrayList.get(i).getQuestionText());
                    textView.append(" \n ");
                    textView.append(" \n ");
                }

            }
        }
    }


    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(MainActivity.this, "Error, something was wrong", Toast.LENGTH_SHORT).show();
    }
});

(in the second activity i am just showing ArrayList contents in TextView)

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
AttiL4682
  • 13
  • 5

1 Answers1

1

If you want to get results from multiple queries, then you should consider using Query#get(), which returns an object of type Task. That being said, to merge 5 different Firebase Realtime Database requests locally, I recommend you to use Tasks.whenAllSuccess() method. You can achieve this, using the following lines of code:

Query questionFromA = FirebaseDatabase.getInstance().getReference()
        .child("A")
        .orderByChild("questionID")
        .equalTo(new Random()
        .nextInt(15));

Query questionFromB = FirebaseDatabase.getInstance().getReference()
        .child("B")
        .orderByChild("questionID")
        .equalTo(new Random()
        .nextInt(15));

//Create the other three queries.

Task aTask = questionFromA.get();
Task bTask = questionFromb.get();

//Create the other three Task objects.

Task combinedTask = Tasks.whenAllSuccess(aTask, bTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
    @Override
    public void onSuccess(List<Object> list) {
         //Do what you need to do with your list
    }
});

And pass all five Task objects to the whenAllSuccess() method. As you can see, when overriding the onSuccess() method the result is a list of objects. So you only have to map each object from the list into an object of type QuizQuestions and do what you need to do with them.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193