Mohd Sakib Syed
Mohd Sakib Syed

Reputation: 1769

How to get particular value using `DataSnapshot` in `Firebase`

I want to get particular value using from DataSnapshot.
I am attaching the screenshot here so kindly check and help me to get particular value from Realtime Database.
enter image description here

Actually I am implementing chat application in which I want to get value of user from group_list. Here is my code.

    private void loadTotalGroupList() {

        referenceMainUrl = FirebaseDatabase.getInstance().getReferenceFromUrl("https://pure-coda-174710.firebaseio.com");
        referenceGroupList = referenceMainUrl.child("group_list");

        //Check if child is available or not.
        referenceGroupList.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {


                    Log.e("dataSnapshot","  ==>"+dataSnapshot);
                    Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();

                    Log.e("newPost","  ==>"+newPost);

                   Log.e("user: ","==>" + newPost.get("user"));  // Here I am getting null value


                } else {
                    Log.e("Child not found", " >>>");
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }  

and log showing like this. DataSnapshot { key = group_list, value = {First Group={-KtBH9gnTszNxcXjNu9A={message=assaasas, user=sakib}}} }

Upvotes: 0

Views: 5541

Answers (3)

Mohd Sakib Syed
Mohd Sakib Syed

Reputation: 1769

I have resolved my issue by using addChildEventListener

    referenceGroupList.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {



            Log.e("dataSnapshot KEY", " ==>" + dataSnapshot.getKey());

        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

Here dataSnapshot.getKey() returns all sub child of it.

Upvotes: 2

Swetabja Hazra
Swetabja Hazra

Reputation: 673

This code runs in a loop and gives you values for all the 'user's

DatabaseReference dRef = FirebaseDatabase.getInstance().getReference().child("group_list").child("First_Group");
    dRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                String userName = snapshot.child("user").getValue(String.class);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317382

You're listening to the location /group_list in your code. That means a a snapshot from that location will contain the following:

/First Group
    /-KtBH9gnTszNxcXjNu9A
        message = "..."
        user = "..."

If you want to get the user value from that location, you'll have to dig into it using each intermediate path:

dataSnapshot.child("First Group").child("-KtBH9gnTszNxcXjNu9A").child("user").getValue()

Or more simply:

dataSnapshot.child("First Group"/-KtBH9gnTszNxcXjNu9A/user").getValue()

You can't skip the middle paths in the snapshot. Alternatively, you may want to listen to a location closer to the value you want:

referenceMainUrl.child("group_list/First Group/-KtBH9gnTszNxcXjNu9A");

Upvotes: 0

Related Questions