2
votes

I am having problem in retrieving data from firebase. I have one root user's node and then inside that root I have one userID child node. Inside that child node I am storing user information according to its blood group.

Now, what I want is to fetch the name and email according to blood group, say fetch all user data whose blood group is B+.

Also, please tell me on how to fetch all entries in the firebase and show on textView.

    //show data on text view
    datashow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("inside click", "onClick: ");
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference db = database.getReference();
            db.child("users").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    if (user == null) {
                        Log.e(TAG, "User data is null!");
                        return;
                    }
                    Iterable<DataSnapshot > children = dataSnapshot.getChildren();
                    Log.i("children", "onDataChange: " + children);
                    for(DataSnapshot child : children)
                    {
                        Map<String , String> map = (Map)child.getValue();
                        Log.i("inside", "onDataChange: " + map);
                        name  = map.get("name");
                        email = map.get("email");
                        dateof = map.get("userDob");
                        showData(name , email  , dateof );
                        Log.i("datasnap", "onDataChange: data snapshot " + name + email + dateof);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });

this is my snapshot for Firebase database

enter image description here

2
just click on "enter image description here " and you will see the snap shot of firebase datarajat singh
You've included a link to a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in your Firebase Database console. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do.Frank van Puffelen
@FrankvanPuffelen thank you for your answer i will definitely add Json tree in my questionrajat singh

2 Answers

1
votes
    db.child("users").orderByChild("userBloodGroup").equalTo("B+").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
             List<User> data = new ArrayList<>();
                    if (dataSnapshot.exists()) {
                for (DataSnapshot snapshot :
                        dataSnapshot.getChildren()) {
                   User element = snapshot.getValue(User.class);
                    element.setKey(snapshot.getKey());
                    data.add(element);

                }
           for (User user: data){
Log.i(TAG,"user name: " +user.getName());
    Log.i(TAG,"user email: " +user.getEmail());
  Log.i(TAG,"user dob: " +user.getUserDob());
 }
                }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i(TAG, "loadPost:onCancelled", databaseError.toException());
        }

    });
0
votes

To query for all users with the bloodGroup B+, use a Firebase Query:

firebaseReference("users").orderByChild("userBloodGroup").equalTo("B+")

To get all the data of the users, you could use a Listener to get all of them and put them in whatever TextView you need. You could check the documentation on Firebase on how they read data. https://firebase.google.com/docs/database/android/read-and-write