0

I want to retrieve my mcq from firebase realtime database in my android app.

Below is the code for the same.

databaseReference = FirebaseDatabase.getInstance().getReference().child("Questions").child(String.valueOf(computerCount));
                 
                databaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        final Question question = dataSnapshot.getValue(Question.class);
                        questionTxt.setText(question.getQuestion());
                        b1.setText(question.getOption1());
                        b2.setText(question.getOption2());
                        b3.setText(question.getOption3());
                        b4.setText(question.getOption4());


                        b1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(b1.getText().toString().equals(question.answer))
                                {
                                    Toast.makeText(getApplicationContext(),"Correct answer",Toast.LENGTH_SHORT).show();
                                    b1.setBackgroundColor(Color.GREEN);
                                    correct = correct +1;
                                    Handler handler = new Handler();

                                    handler.postDelayed(new Runnable() {
                                        public void run() {
                                            b1.setBackgroundColor(Color.parseColor("#03A9F4"));
                                            updateQuestion();
                                        }
                                    }, 1500);

                                }

I want this to fetch in random way instead of sequential way ex: 1,2

my json is like :
Structure

Haider Saleem
  • 773
  • 1
  • 9
  • 17
  • What exactly in this code doesn't work the way you expect? Please also add your database structure as a JSON file or at least a screenshot. – Alex Mamo Sep 22 '20 at 07:15
  • my json is like this https://i.stack.imgur.com/CijIR.png i want this to fetch in random way instead of sequential way ex: 1,2,3 – AlphaVersion Sep 22 '20 at 07:38

1 Answers1

0

you can retrieve all of your mcq's and saved them in an arraylist and later using

Collections.shuffle(arrayList);

you can shuffle them. To know more about shuffle and usage you can follow this link

Haider Saleem
  • 773
  • 1
  • 9
  • 17