SixinL
SixinL

Reputation: 15

How to retrieve specific data from firebase realtime database when data changed

I am creating the function that the user can track the driver's location. I want to retrieve the latitude and longitude only when the driver's location change.

Database structure

Firebase Realtime Database

driver.java(added setter and getter)

public class driver {
    private String Email, BusID, Carplate, PhoneNo, Password, Latitude, Longitude;

    public driver(String Email, String BusID, String Carplate, String PhoneNo, String Password,
                  String Latitude, String Longitude){
        this.Email = Email;
        this.BusID = BusID;
        this.Carplate = Carplate;
        this.PhoneNo = PhoneNo;
        this.Password = Password;
        this.Latitude = Latitude;
        this.Longitude = Longitude;
    }
    public String getEmail() {
        return Email;
    }
    public void setEmail(String email) {
        Email = email;
    }
    public String getBusID() {
        return BusID;
    }
    public void setBusID(String busID) {
        BusID = busID;
    }
    public String getCarplate() {
        return Carplate;
    }
    public void setCarplate(String carplate) {
        Carplate = carplate;
    }
    public String getPhoneNo() {
        return PhoneNo;
    }
    public void setPhoneNo(String phoneNo) {
        PhoneNo = phoneNo;
    }
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }

    public String getLatitude() {
        return Latitude;
    }

    public void setLatitude(String latitude) {
        Latitude = latitude;
    }
    public String getLongitude() {
        return Longitude;
    }
    public void setLongitude(String longitude) {
        Longitude = longitude;
    }

Java

databaseReference = FirebaseDatabase.getInstance().getReference("Driver");

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                driver loc = dataSnapshot.getValue(driver.class);
                if (loc != null) {
                    Log.d("Location", "" +loc.getLatitude()+ ", " +loc.getLongitude());
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

How I can get the data from the database? Please help me. Thanks

Upvotes: 1

Views: 97

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Change your class to this:

public class Driver {
    private String email, busID, carplate, phoneNo, password, latitude, longitude;

    /** No-arg constructor (takes no arguments). */
    public Driver() {
    }

    public Driver(String email, String busID, String carplate, String phoneNo, String password,
                  String latitude, String longitude){
        this.email = email;
        this.busID = busID;
        this.carplate = carplate;
        this.phoneNo = phoneNo;
        this.password = password;
        this.latitude = latitude;
        this.longitude = longitude;
    }

You need to follow the java bean convention, then when adding data, create a new Driver object and use it inside of setValue():

Driver dirver = new Driver(email, busID, carplate, phoneNo, password,latitude,longitude);
mDatabase.child(userId).setValue(user);

Then to retrieve the data, do the following:

databaseReference = FirebaseDatabase.getInstance().getReference("Driver");

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                  Driver driver = dataSnapshot.getValue(Driver.class);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

https://firebase.google.com/docs/database/android/read-and-write

Upvotes: 1

Related Questions