Home > Software engineering >  Only some fields from Firebase are transferred to my code in Android Studio
Only some fields from Firebase are transferred to my code in Android Studio

Time:11-05

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?

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!

CodePudding user response:

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

public Object getAmBusRoute() { return amBusRoute; }

public Object getPmBusRoute() { return pmBusRoute; }

CodePudding user response:

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());
        }
    }
});
  • Related