如何从Firebase数据库中检索全部数据?
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
info info = dataSnapshot.getValue(info.class);
// [START_EXCLUDE]
tw_mail.setText(info.email);
tw_Address.setText(info.address);
tw_fact.setText(info.fact);
tw_ownername.setText(info.ownername);
tw_phone.setText(info.phone);
tw_pin.setText(info.pin);
tw_rent.setText(info.rent);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(locatepg.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
}
};
mDatabase.addValueEventListener(postListener);
答案 0 :(得分:0)
只需致电DatabaseReference.getRoot()
firebaseDatabase.getRoot().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot.getChildrenCount()); // These are all your roots
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
答案 1 :(得分:-2)
请您必须像这样设置要从中检索数据的孩子:
第二次选择您的孩子。
FirebaseDatabase.getInstance().getReference().child("user").child(getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(view.getContext(),"database error",
Toast.LENGTH_SHORT).show();
}
});