简体   繁体   中英

Get Value of Key Based on the Parent Child of Current User Firebase Android

I'm trying to retrieve the value in ServiceFee . I can't do a direct

FirebaseDatabase.getInstance().getReference()
            .child("RequestService").child("Plumbers").child(uid)

Because the child below RequestService (like Plumbers) should depend based on the Parent Child of the current user found in Workers

As an example shown in the screenshot, user KO2rC.. 's parent child is Plumbers , so I should get that first before I can get the ServiceFee value

I tried the technique in my previous post but I can't seem to recover the value, only the key and I don't need the key

Try this:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String uid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Workers");
ref.addSingleValueEventListener(new ValueEventListener(){
  @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
     for(DataSnapshot datas: dataSnapshot.getChildren()){
        String plumbers=datas.getKey();

    DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("RequestService").child(plumbers).child(uid);
    ref.addSingleValueEventListener(new ValueEventListener(){
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
        String fees=dataSnapshot.child("ServiceFee").getValue().toString();
     }
     @Override
     public void onCancelled(FirebaseError firebaseError) {

       }
    });
  }

    @Override
  public void onCancelled(FirebaseError firebaseError) {

   }
});

You can do the above, first add the snapshot at child("Workers") ,then iterate and retrieve the value which is Plumbers using getKey() .

After that add a nested listener and put the retrieved Plumbers inside the child child("RequestService").child(plumbers) and retrieve the value ServiceFee

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM