如何识别随机ID并将其称为子代?我需要在摘录中将此ID称为:

favoritosRef = firebaseRef.child("favoritos").child( idUser )


java - 如何识别随机ID并将其称为子代?-LMLPHP

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) {}
    });
}

最佳答案

您没有得到任何东西,因为您仅使用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);

关于java - 如何识别随机ID并将其称为子代?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51486638/

10-10 06:58