Get Parent Name Using Child Key In Firebase Android
I am trying to get parent name '170002001' by using child key 'UID' in android studio. Please help. Thanks in advance :) String uid = user.getUid(); private void getdata(){
Solution 1:
Place this inside your onDataChange(DataSnapshot dataSnapshot)
listener
Stringparent= dataSnapshot.getKey();
Since firebase will manage a list of possible parent keys for that , do this
DatabaseReference reference;
reference = FirebaseDatabase.getInstance.getReference();
reference.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshotchildSnapshot: dataSnapshot.getChildren()) {
String parent = childSnapshot.getKey();
Log.i(TAG,parent);
}
}
note that reference here will be the node up above your "170002001"
key, which is the parent of all those keys
Edit: If you want to get all UID inside each of those nodes do this
Create a POJO class to hold that user id
publicclassUserPojo {
privateStringUID;
publicUserPojo(StringUID) {
this.UID = UID;
}
publicUserPojo() {
}
publicStringgetUID() {
returnUID;
}
publicvoidsetUID(String UID) {
this.UID = UID;
}
}
then just do the same like above
reference.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
Stringparent= childSnapshot.getKey();
UserPojouser= childSnapshot.getValue(UserPojo.class);
//get your User IDStringuser= user.getUID();
Log.i(TAG,parent);
}
}
Post a Comment for "Get Parent Name Using Child Key In Firebase Android"