简体   繁体   中英

How to retrieve Specific Data from Firebase Realtime Database?

I have been trying to retrieve data from Firebase Realtime Database, but somewhere it's going wrong.

Here is the screenshot of database

I want to retrieve all the Question(Question1,Question2,etc) data under "CS102" node and store it in arraylist. I tried doing it in a few ways:

reference = FirebaseDatabase.getInstance().getReference().child("CreateQuizQuestions").child(code);
reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
        if(snapshot.exists()) {
            for(DataSnapshot ds:snapshot.getChildren()){
                String QuizName=ds.child("QuizName").getValue().toString();
                String TotalQuestions=ds.child("TotalQuestions").getValue().toString();
                String ques=ds.child("Question").getValue().toString();
                String option1=ds.child("Option1").getValue().toString();
                String option2=ds.child("Option2").getValue().toString();
                String option3=ds.child("Option3").getValue().toString();
                String option4=ds.child("Option4").getValue().toString();
                String correctAnswer=ds.child("CorrectAnswer").getValue().toString();

                GetQuestion getQuestion=new GetQuestion(ques,option1,option2,option3,option4,correctAnswer);
                list.add(getQuestion);

                Log.d("Data Values: ","Question:"+list.get(0).question+" Option1: "+list.get(0).option1);
                Log.d("Inner Size Tag","Size of list: "+list.size());
            }
        } else {
            Toast.makeText(getApplicationContext(),"No Data",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCancelled(@NonNull @NotNull DatabaseError error) {

    }
});

But somewhere it's going wrong Please help me in finding the solution

Like this, you should be able to gather the info of all "Question1" nodes. You will have to do this for each "Question2", "Question3" node. If for example "Question2" node has different children, you won't have to use same number of variables to retrieve the data. Of course you must add another event listener because database reference will be different. If your other question children have same number of children (answer, optrion1, 2,3,4 and question) you can use the for loop I have shown you before. Hope this works for you!!!

 DatabaseReference  ref = FirebaseDatabase.getInstance().getReference().child("CreateQuizQuestions").child("CS102").child("Question1");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                        if(snapshot.exists())
                        {
                        

                            
                       String  answer=snapshot.child("Answer").getValue().toString();
                            
                           
                         String  op1=snapshot.child("Option1").getValue().toString();
                         String  op2=snapshot.child("Option2").getValue().toString();
                         String  op3=snapshot.child("Option3").getValue().toString();
                         String  op4=snapshot.child("Option4").getValue().toString();
                         String  q=snapshot.child("Question").getValue().toString();                                

                            //GetQuestion getQuestion=new GetQuestion(ques,option1,option2,option3,option4,correctAnswer);
                            //list.add(getQuestion);

                            //Log.d("Data Values: ","Question:"+list.get(0).question+" Option1: "+list.get(0).option1);
                            //Log.d("Inner Size Tag","Size of list: "+list.size());                     
                              
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(),"No data",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull @NotNull DatabaseError error) {

                    }
                });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM