1

I am building my first project in android using Firebase.

I have build a Firebase's database which is having main node as post, media as its child node, media node can contain multiple values for a particular post or can have one value inside it.

So i want to fetch media child node values with particular post node.

Could you please help me out.

Any help will deeply appreciated.

Please check the screenshots below.

Database image

4 Answers 4

1
Firebase ref = new Firebase(YOUR_FIREBASE_URL);
            String postNode ="KddShng........";
            ref.child("posts/"+postNode).child("media").addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        Log.e("!_@@_Key : >",dataSnapshot.getKey()); // you will get key of Media node
                }

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

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

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

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
Sign up to request clarification or add additional context in comments.

2 Comments

i want to fetch the post node as well and inside that i want media node list. would this do the same ?
please Expand that node also.. put new image for it..so i can get idea
1

First you need to read docs to know how to retrieve data from firebase.

and your data will be like this ...

final Firebase ref = new Firebase("URL/posts");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
//u can handle your variables name in GetData class
                GetData posts = userSnapshot.getValue(GetData.class);

In another way..

                DatabaseReference  databaseReference = FirebaseDatabase.getInstance().getReference().child("posts");
                String user_id = firebaseAuth.getCurrentUser().getUid();
               databaseReference.child(user_id).child("media").getValue("user_id").getValue(etc);

You might save last line as String to reuse it anywhere.

UPDATE:

And to Fitch data You have to use Firebase adapter, example

3 Comments

i need post data and media child node data for particular post in single getset array. How to achieve that functionality?
Can you please send me github link for the same or any working example?
please check Update .
0

Try something like this

FirebaseDatabase.getInstance().getReference("posts").child(UID).child("media");

Where UID is the key of the particular post node.

PS: this returns something like a promise, for which you need to attach a listener for. (because this is real time)

Comments

0

If just want to fetch the value once, you can do like this.

FirebaseDatabase.getInstance().getReference("posts").child("your_post_item_id").child("media").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

              for (DataSnapshot children : dataSnapshot.getChildren()) {
                  YourMediaModel mediaItem = children.getValue(YourMediaModel.class);
                  //add you mediaItem to list that you provided
              }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.