如何识别随机ID并将其称为子代?我需要在摘录中称呼此ID:
favoritosRef = firebaseRef.child("favoritos").child( idUser )
https://developer.apple.com/documentation/foundation/nsfilemanager
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) {}
});
}
答案 0 :(得分:1)
如果您需要当前用户的ID,可以致电:
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
然后
userID = user.getUid();
或者,如果它是您需要的随机pushID(用户ID的子对象),则需要在首次创建pushID引用时将其另存为变量:
DatabaseReference pushRef = databseRef.push();
String pushKey = pushRef.getKey();
所以您以后可以查询。.
答案 1 :(得分:1)
您什么也没得到,因为您仅使用一次getChildren()
。为了解决这个问题,您需要像这样循环两次:
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);