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(){
DatabaseReference reference;
reference = FirebaseDatabase.getInstance().getReference();
reference.orderByChild("UID").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
keys = childSnapshot.getKey();
status.setText(keys);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Edit: Except "170002001" i am able to retrieve all parent name. :(
Place this inside your onDataChange(DataSnapshot dataSnapshot) listener
String parent = dataSnapshot.getKey();
Since firebase will manage a list of possible parent keys for that , do this
DatabaseReference reference;
reference = FirebaseDatabase.getInstance.getReference();
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: 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
public class UserPojo {
private String UID;
public UserPojo(String UID) {
this.UID = UID;
}
public UserPojo() {
}
public String getUID() {
return UID;
}
public void setUID(String UID) {
this.UID = UID;
}
}
then just do the same like above
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String parent = childSnapshot.getKey();
UserPojo user = childSnapshot.getValue(UserPojo.class);
//get your User ID
String user = user.getUID();
Log.i(TAG,parent);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With