这是我编写的代码,但是它返回了整个架构。
mDatabase = FirebaseDatabase.getInstance().getReference().child("/News");
mTitle = (TextView)findViewById(R.id.title_view);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String title=dataSnapshot.getValue().toString();;
mTitle.setText(title);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});
答案 0 :(得分:2)
要解决此问题,只需使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference newsRef = rootRef.child("News");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String desc = ds.child("desc").getValue(String.class);
String title = ds.child("title").getValue(String.class);
Log.d(TAG, desc + " / " + title);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
newsRef.addListenerForSingleValueEvent(valueEventListener);
您的logcat中的输出将是:
desc1 / title1
desc2 / title2
答案 1 :(得分:1)
use this method
//-----------------//
private void getDataFrom() {
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference mCustomerDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child("1");
mCustomerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists() && dataSnapshot.getChildrenCount() > 0) {
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
if (map.get("title") != null) {
customer_name = map.get("title").toString();
Log.i("customer_name",String.valueOf(customer_name));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案 2 :(得分:0)
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference db = databaseReference.child("Customers");
arrayList = new ArrayList<>();
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
arrayList.add(snapshot.getValue(Customer_Details2.class));
}
checkUpdate();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});