我有以下firebase数据库,我无法检索名称,地址,否等详细信息

时间:2017-09-07 11:03:01

标签: android firebase firebase-realtime-database

enter image description here

以下是来自firebase数据库的图片,我无法检索数据。

MainActivity.java

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    // This method is called once with the initial value and again
    // whenever data at this location is updated.

    FirebaseData pg=dataSnapshot.getValue(FirebaseData.class);
    Log.d(TAG, "Value is: " + value);
    Toast.makeText(MainActivity.this,value,Toast.LENGTH_SHORT).show();
}

FirebaseData.java类

public class FirebaseData {
    public String name;
    public String address;
    public String pgid;
    public String contact_no;

    public FirebaseData() {
        // Needed for Firebase
    }

    public FirebaseData(String address, String name, String contact_no) {
        this.name = name;
        this.address = address;
        this.contact_no=contact_no;
    }

2 个答案:

答案 0 :(得分:1)

要从address节点获取contact_nonameHSRLayout,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference HSRLayoutRef = rootRef.child("cities").child("Bangalore").child("localities").child("HSRLayout");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String address = dataSnapshot.child("1").child("address").getValue(String.class);
        String contact_no = dataSnapshot.child("1").child("contact_no").getValue(String.class);
        String name = dataSnapshot.child("1").child("name").getValue(String.class);
        Log.d("TAG", address + " / " + contact_no + " / " + name);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
HSRLayoutRef.addListenerForSingleValueEvent(eventListener);

您的输出将是:

das / 15615446484 / HSR

如果您想从多个孩子那里获取数据,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference HSRLayoutRef = rootRef.child("cities").child("Bangalore").child("localities").child("HSRLayout");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String address = ds.child("address").getValue(String.class);
            String contact_no = ds.child("contact_no").getValue(String.class);
            String name = ds.child("name").getValue(String.class);
            Log.d("TAG", address + " / " + contact_no + " / " + name);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
HSRLayoutRef.addListenerForSingleValueEvent(eventListener);

答案 1 :(得分:0)

在数据库检查规则和设置

{ "rules": {
".read": true,
".write": true}}