Ravi Prakash
Ravi Prakash

Reputation: 44

How to Read Data From Firebase database

I want to read data from my Firebase database for all questions. For e.g. for question1 I want to store ques,ans and answeredBy into three variables. What I am currently doing is here.

private FirebaseDatabase database=FirebaseDatabase.getInstance();
private DatabaseReference 
quizRef=database.getReference("Quiz").child("Questions");
quizRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds:dataSnapshot.getChildren())
            {
                ModelClass modelClass=new ModelClass();
                modelClass.setQues(ds.child("question1").getValue(ModelClass.class).getQues());
                modelClass.setAns(ds.child("question1").getValue(ModelClass.class).getAns());

                textViewQuestionText.setText(modelClass.getQues());
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            textViewQuestionText.setText("Error occurred");

        }
    });

enter image description here

Upvotes: 0

Views: 808

Answers (3)

PradyumanDixit
PradyumanDixit

Reputation: 2375

You can just use the data from your Firebase Database directly using the following code:

DatabaseReference q1Ref = quizRef.child("question1");
q1Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            String question = dataSnapshot.child("ques").getValue(String.class);
            String answer = dataSnapshot.child("ans").getValue(String.class);
            int ansBy = dataSnapshot.child("answeredBy").getValue(Integer.class);

            // now you can do what you want with the values stored in the variables
        }

        @Override
        public void onCancelled(DatabaseError error) {
            textViewQuestionText.setText("Error occurred");

        }
    });

Upvotes: 1

xaif
xaif

Reputation: 563

To read or write data from the database, you need an instance of DatabaseReference:

private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();

To read data at a path and listen for changes, use the addValueEventListener() or addListenerForSingleValueEvent() method to add a ValueEventListener to a DatabaseReference.

You can use the onDataChange() method to read a static snapshot of the contents at a given path, as they existed at the time of the event.

For more information you can refer to firebase docs

Upvotes: 0

Zaid Mirza
Zaid Mirza

Reputation: 3699

Tried and Tested. You dont have to check for keys like question1,question2 etc

       FirebaseDatabase    database = FirebaseDatabase.getInstance();
        DatabaseReference reference = database.getReference("Quiz/Questions");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot ds: dataSnapshot.getChildren()) {

                    ModelClass modelClass = (ds.getValue(ModelClass.class));
                    modelClass.getAns();
                    modelClass.getQes();
                    // fetch any key value pair
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

I recommend to use firebase generated hash keys instead of your own keys like question1,question2 etc

Check this

Upvotes: 2

Related Questions