LizG
LizG

Reputation: 2530

android: get the child of child

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

Upvotes: 4

Views: 249

Answers (3)

Khaled Lela
Khaled Lela

Reputation: 8139

DatabaseReference historyRef = FirebaseDatabase.getInstance().getReference().child("History");

    historyRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()){
                Log.d(TAG, "getKey: key = " + child.getKey());

                Ride ride = child.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) {

        }
    });
}

//
//       "driver" : "ptnVOKounjXE9VrmZCCvKoZWluf1",
//               "rating" : 0,
//               "ridePrice" : 5.25,
//               "rider" : "C0RjB5NPZcTvWz9XiUAhpTDOK0C2",
//               "status" : "accepted",
//               "timestamp" : 1530662726

class Ride{
    // Remove @Exclude https://firebase.google.com/docs/reference/android/com/google/firebase/database/Exclude
    private String driver;
    private int rating;
    private float ridePrice;
    private String rider;
    private String status;
    private long timestamp;

    public Ride() {
      // Empty Constructor  
    }
      // Getter & Setter 
  }

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 139039

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.

Upvotes: 2

Ticherhaz FreePalestine
Ticherhaz FreePalestine

Reputation: 2387

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);

        DatabaseReference statusRef = FirebaseDatabase.getInstance().getReference("History").Child(rideKey).Child("status");
statusRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            History history = new History(); //If you make class for history
            history = dataSnapshot.getValue(History.class);
            textViewStatus.setText(history.getStatus());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Upvotes: 0

Related Questions