Reputation: 147
How to identify random id and call it as child? I need to call this id in the excerpt:
favoritosRef = firebaseRef.child("favoritos").child( idUser )
favoritosRef = firebaseRef.child("favoritos").child( idUsuario );
valueEventListenerFavoritos = favoritosRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
perfil.clear();
for (DataSnapshot dados : dataSnapshot.getChildren()) {
Perfil perfil2 = dados.getValue(Perfil.class);
perfil.add(perfil2);
}
adapterPerfil.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
Upvotes: 1
Views: 61
Reputation: 139019
You are not getting anything because you are using getChildren()
only once. To solve this, you need to loop twice like this:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("favoritos").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Perfil> list = new ArrayList<>();
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
Perfil perfil = ds.getValue(Perfil.class);
Log.d("TAG", perfil.getTitulo());
}
}
//Set the list to adapter
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
Upvotes: 1
Reputation: 252
If its the current user's ID you need, you can call:
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
then
userID = user.getUid();
Or, if it's the random pushID that you need (the child of the userID), you need to save the pushID reference as a variable when it is it is first created:
DatabaseReference pushRef = databseRef.push();
String pushKey = pushRef.getKey();
So you can query it later..
Upvotes: 1