简体   繁体   中英

retrieve some data from the firebase database with some nodes being null

在此处输入图片说明

cBankRef=myRef.child("user_id").child("ASSETS");


 cBankRef.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                        String value = dataSnapshot.child("1").child("description").getValue(String.class);
                        Log.d(TAG, "onChildAdded: the value is"+value);


                        if(dataSnapshot.child("2").exists()) {
                            String valuess = dataSnapshot.child("3").child("description").getValue(String.class);
                            Log.d(TAG, "onChildAdded: the value is: " + valuess);

                        }
                        if (dataSnapshot.child("3").exists()) {
                            String values = dataSnapshot.child("1").child("description").getValue(String.class);
                            Log.d(TAG, "onChildAdded: the cash at bank is: "+values);
                        }

                        }



                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

first i accessed the user_id child and the ASSETS then through the nodes ("1"),("2") i had to add other nodes in order to get the value, the("1") and ("2") were ("Cash at Bank") ("stock") in the beginning but i decided to change them to numbers but it's giving null @makrand pawar

You have multiple issues in your example:

  • Your cBankRef is pointing to \\user_id\\ASSETS\\Cash at bank\\stock\\Cash in Hand , which does not exist ( Cash at bank node does not exist under ASSETS ).
  • You are then attaching a ChildEventListener to this reference, which won't return any values, because there aren't any children under it (as it doesn't exist).
  • Inside onChildAdded() , you are trying to access the houses node which also doesn't exist in your example ( house does though).

To access the children under the Stock node, you'll need something like:

ref = FirebaseDatabase.getInstance().getReference().child("user_id").child("ASSETS").child("Stock");

ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String value = dataSnapshot.child("house").child("values").getValue(String.class);
        Log.d(TAG, "onChildAdded: the value is: "+value);
    }
    // ...
});

The DatabaseReference#child() method is not a SELECT statement. To access everything underneath a node, you only need to create a reference to the highest node required, then attach a listener to that.

For example, if you need access to everything underneath the ASSETS node, you could attach a listener there instead:

ref = FirebaseDatabase.getInstance().getReference().child("user_id").child("ASSETS");

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String value = dataSnapshot.child("Stock").child("house").child("values").getValue(String.class);
        Log.d(TAG, "onChildAdded: the value is: "+value);

        if (dataSnapshot.child("Cash at bank").exists()) {
            String value = dataSnapshot.child("Cash at bank").getValue(String.class);
            Log.d(TAG, "onChildAdded: the cash at bank is: "+value);
        }
    }
    // ...
});

Then when the listener returns you can use DataSnapshot#child() instead to access the individual values retrieved from that location. Also, using DataSnapshot#exists() is a simple way to check that a node exists before using it.

If you plan to have additional values or nodes under ASSETS that don't currently exist, they will be returned by the above listener as soon as they are created.

To achieve this, i suggest you using ValueEventListener like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference stockRef = rootRef.child("user_id").child("ASSETS").child("Stock");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String values = ds.child("house").child("values").getValue(String.class);
            Log.d("TAG", values);
        }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM