Only some fields from Firebase are transferred to my code in Android Studio

Viewed 64

I've spent hours trying to figure out why some of the fields from a document in Firestore won't transfer to my code.

Can you please help me to identify what's wrong with my code and how I should change it to make it work?

This is what my Firebase Database looks like

(And yes, the bus numbers are purposely saved as Strings)

I was trying to use the getValue method to read the data from Firebase as shown in the code below:

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("BusDrivers");

  // Getting all of the registered bus drivers' information
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot datasnapshot) {
            for (DataSnapshot snapshot : datasnapshot.getChildren()) {
                BusDriver busDriver = snapshot.getValue(BusDriver.class);
                busDrivers.add(busDriver);
            }
        }

However, only some of the fields are transferred to the busDriver object. When I call busDriver.getDriverFirstName(), I accurately get a result...

Don is returned after calling for the driver's first name

But when I call busDriver.getAmBusRoute(), I get this for some reason...

N/A

Here's the code of the BusDriver class as well.

public class BusDriver {
private final String busDriverID;
private final String amBusRoute;
private final String pmBusRoute;
private final String driverFirstName;
private final String driverMiddleName;
private final String driverLastName;

public BusDriver() {
    this.busDriverID = "N/A";
    this.amBusRoute = "N/A";
    this.pmBusRoute = "N/A";
    this.driverFirstName = "N/A";
    this.driverMiddleName = "N/A";
    this.driverLastName = "N/A";
}

public BusDriver(String busDriverID, String amBusRoute, String pmBusRoute, String driverFirstName, String driverMiddleName,
                 String driverLastName) {
    this.busDriverID = busDriverID;
    this.amBusRoute = amBusRoute;
    this.pmBusRoute = pmBusRoute;
    this.driverFirstName = driverFirstName;
    this.driverMiddleName = driverMiddleName;
    this.driverLastName = driverLastName;
}

public String getBusDriverID() { return busDriverID; }

public Object getAmBusRoute() { return amBusRoute; }

public Object getPmBusRoute() { return pmBusRoute; }

public String getDriverFirstName() { return driverFirstName; }

public String getDriverMiddleName() { return driverMiddleName; }

public String getDriverLastName() { return driverLastName; }

}

Please help me. Thank you!

2 Answers

As I see in your screenshot, you are using Firestore, but your code tries to read data from the Realtime Database, and this is not correct. To solve this problem, you have to add Firestore to your project. To achieve this, please check the official documentation regarding how to get started with Cloud Firestore.

Right after you do that, to read the data you can use the following code:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference busDriversRef = db.collection("BusDrivers");
busDriversRef..get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                BusDriver busDriver = document.toObject(BusDriver.class);
                busDrivers.add(busDriver);
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

You are returning an Object from these getters. You should be returning String here.

public Object getAmBusRoute() { return amBusRoute; }

public Object getPmBusRoute() { return pmBusRoute; }
Related