android: get the child of child

LizG :

I am trying to get the "status" of the ride that is presently in my "History" node but first I had to get the "rideKey".

Global variables: String rideKey, String key.

History node:

{
  "History" : {
    "-LGXaukR30LTjrL3ZNpt" : {
      "driver" : "ptnVOKounjXE9VrmZCCvKoZWluf1",
      "rating" : 0,
      "ridePrice" : 5.25,
      "rider" : "C0RjB5NPZcTvWz9XiUAhpTDOK0C2",
      "status" : "accepted",
      "timestamp" : 1530662726
    }
  }
}

To get the rideKey, I did this:

    DatabaseReference keyRef = FirebaseDatabase.getInstance().getReference("History");
    keyRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            rideKey = String.valueOf(mDatabase.child("History").push().getKey());
            Log.d(TAG, "getKey: key = " + rideKey);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

This gives me all the keys in History, how can I get the most recent?

Now, I, also need to get the "status" of the request.

But, when I try to get the status, it keeps coming up null.
I have tried putting another ValueEventListener inside the rideKey value event but still null

Any ideas as to what I am doing wrong? Much appreciated.

EDIT

Log.e(TAG, "I made it to getKeyAndStatus");

    DatabaseReference keyRef = FirebaseDatabase.getInstance().getReference("History");
    keyRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
**// Log.e(TAG, "I made it to getKeyAndStatus: onDataChange");**

            Iterable<DataSnapshot> children = dataSnapshot.child("History").getChildren();

            for (DataSnapshot child : children){
                Log.d(TAG, "getKey: key = " + child.getKey());

                Ride ride = child.getValue(Ride.class);
                Log.e(TAG, "ride = " + ride);

                Log.d(TAG, "Driver = " + ride.getDriver());
                Log.d(TAG, "Rating = " + ride.getRating());
                Log.d(TAG, "Rider = " + ride.getRider());
                Log.d(TAG, "Price = " + ride.getRidePrice());
                Log.d(TAG, "status = " + ride.getStatus());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

The above code only makes it as far as the Log "I made it to getKeyAndStatus: onDataChange"

EDIT - Results

results

Alex Mamo :

To solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference historyRef = rootRef.child("History");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Ride ride = ds.getValue(Ride.class);

            Log.d(TAG, "Driver = " + ride.getDriver());
            Log.d(TAG, "Rating = " + ride.getRating());
            Log.d(TAG, "Rider = " + ride.getRider());
            Log.d(TAG, "Price = " + ride.getRidePrice());
            Log.d(TAG, "status = " + ride.getStatus());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
historyRef.addListenerForSingleValueEvent(valueEventListener);

The output in the logcat will be the values of your properties.

Note, when using the following line of code:

rideKey = String.valueOf(mDatabase.child("History").push().getKey());

You are generating another key instead of getting the existing one. Use the push() method only when you add objects not when you read them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=82660&siteId=1