在Uid下检索特定密钥并用于查询firebase Android

时间:2017-02-16 02:38:20

标签: android firebase firebase-realtime-database

我正在尝试使用特定节点中某个键的值进行Firebase查询。特定节点是当前已登录的用户。

如何在当前登录的用户节点下获取特定密钥的值并使用它来查询我的数据库?

这是我的代码:

    final String user_id = firebaseAuth.getCurrentUser().getUid();

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
            halls1 = (String) dataSnapshot.child("Male").child(user_id).getValue(User.class).getHalls();
            halls2 = (String) dataSnapshot.child("Female").child(user_id).getValue(User.class).getHalls();
         }
        @Override
        public  void onCancelled(DatabaseError databaseError){

        }
    });


    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.child("Male").hasChild(user_id)){
                query = databaseReference.child("Female").orderByChild("halls").equalTo(halls1);

            }
            else{
                query = databaseReference.child("Female").orderByChild("halls").equalTo(halls2);
            }
        }
        @Override
        public  void onCancelled(DatabaseError databaseError){

        }
    });

数据库结构

{
  "Users" : {
    "Female" : {
      "WwBjblOo8maSP5PXlYviONrS3Oo2" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2Fecf2758a-5628-4297-bcb3-e8c3f138cbbd.jpeg?alt=media&token=1ca6067d-6c08-49b9-a2bf-6a8315ada067",
        "halls" : "Kwapong",
        "levels" : "300",
        "username" : "merylstre"
      },
      "X8c80zaY0kfT5KVoTFHbhPIEG9z2" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F51dc26a1-45e8-4897-9c2f-14caf8e79cc5.jpeg?alt=media&token=90818c1e-167e-4799-bc29-b3fe52529cd2",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "hashcodef"
      }
    },
    "Male" : {
      "2EXejmHoMlWoKSDb5APvSt8do5e2" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F83ca18a0-63a5-477a-ab1e-b2732b3ebfa1.jpeg?alt=media&token=b8626221-a67c-48e3-b958-d373c9f63c69",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "fillman"
      },
      "BW79oGosAIhdxDxYXbtHbSbC0D23" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F90b93b19-230b-4f28-b394-358d0c516c5c.jpeg?alt=media&token=0c82c272-d6fb-46e8-a6cf-581f5501de7b",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "radjames"
      },
      "Mop4hbS0zne6QS3woum1udLgwqf1" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F78621a8b-7d67-413c-9e57-f378e60ba41e.jpeg?alt=media&token=3982ee0a-2a34-41b3-9250-f0884531ee86",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "radjohn"
      },
      "VvnOY3k7Cwa36bJW55JiZDJRwPN2" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F1ac7080f-8db8-49a7-bb76-34b007deb839.jpeg?alt=media&token=6d100527-80d3-49b3-b0ae-617dd55e64de",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "ciryadams"
      },
      "WOT11DVkoVb9owzK4qKHbvT4ODF2" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F3fda02ae-c577-4a4a-86c1-cadcaf518c97.jpeg?alt=media&token=5baf2a79-b14c-4e28-b7bb-77e55ae08e55",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "markzuck"
      },
      "dbnmFoC4hnSCsXsxJgVO0Q7GYx13" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F2366c787-fb16-4049-9c7d-3d0c2a09ab9b.jpeg?alt=media&token=25bda8dd-ab24-4b9b-8299-534e49bf9ed4",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "dasdsada"
      },
      "iqZfSgGNpPZ4HDtOUuHYZ1u8x273" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2Fc52cd467-05b9-4ece-8085-4cdf502839c5.jpeg?alt=media&token=4c9eb36e-4644-49ec-85eb-c2fef38ca50e",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "maameEsi"
      },
      "jj9yycmDNbZwiPVTLrRyYZheM702" : {
        "downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F5c4a34b2-bc7b-43e7-8b00-7e0bf63ff252.jpeg?alt=media&token=59a11905-a67d-48ea-9f16-10ffd3915876",
        "halls" : "Sarbah",
        "levels" : "100",
        "username" : "hashcode"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果用户位于Male节点下

,请阅读该用户
final String user_id = firebaseAuth.getCurrentUser().getUid();
final DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = root.child("Male").child(user_id);

userRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot){
        if (snapshot.exists()) {
            halls1 = (String) snapshot.getValue(User.class).getHalls();
        }
        else {
            System.out.println("User "+user_id+" does not exist under /Male");
        }

     }
    @Override
    public  void onCancelled(DatabaseError databaseError){

    }
});