数据更改后如何从Firebase实时数据库中检索特定数据

时间:2020-02-13 16:47:02

标签: android firebase firebase-realtime-database

我正在创建用户可以跟踪驾驶员位置的功能。我只想在驾驶员的位置发生变化时才检索纬度和经度。

数据库结构

Firebase Realtime Database

driver.java(添加了setter和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) {

            }
        });

如何从数据库中获取数据? 请帮我。谢谢

1 个答案:

答案 0 :(得分:1)

将您的课程更改为此:

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

您需要遵循java bean convention,然后在添加数据时创建一个新的Driver对象并在setValue()内使用它:

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

然后要检索数据,请执行以下操作:

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