简体   繁体   中英

How to get child object's value inside a key from a list of data (using childeventlistener in firebase: android studio)

这是Firebase JSON结构

Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener

 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

    lastlastref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String string = dataSnapshot.getValue(String.class);


        }

        @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) {

        }
    });

and then I've tried using EventListener to get the values inside of it

 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

    lastlastref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String string = dataSnapshot.getValue(String.class);

            DatabaseReference newRef = lastlastref.child(string);

            newRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    note = snapshot.child("note").getValue(String.class);
                    vaccine = snapshot.child("vaccine").getValue(String.class);
                    timestamp = snapshot.child("date_time").getValue(String.class);

                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
             arrayList.add(vaccine + "" +  timestamp + "" + note);
             adapter.notifyDataSetChanged();

        }

        @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) {

        }
    });

But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"

You set up your reference to:

lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

So each child node under this location is a JSON object like this:

{
  "date_time": "November/16/2018...",
  "note": "hhjj...",
  "vaccine": "Measles"
}

But in your onChildAdded , you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class) returns null .

To get the values, you can call getValue() on the individual properties:

lastlastref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String date_time = dataSnapshot.child("date_time").getValue(String.class);
        String note = dataSnapshot.child("note").getValue(String.class);
        String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
    }

You can also create a minimal class to wrap each record, and read that. The simplest version of that is:

public class ImmunizationRecord {
  public String date_time;
  public String note;
  public String vaccine;
}

And the reading would then be done with:

public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    String record = dataSnapshot.getValue(ImmunizationRecord.class);
}

A small side note about the way you store date_time . This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (eg 2018-11-16T11:29:00 ).

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