Fetch data from nested nodes in Firebase

依然范特西╮ 提交于 2019-12-23 02:28:24

问题


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.


回答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) {

                }
            });



回答2:


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:


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)




回答4:


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) {

        }


来源:https://stackoverflow.com/questions/42408913/fetch-data-from-nested-nodes-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!