【问题标题】:Retrieving data from Firebase DB and storing in local variable: Android从 Firebase DB 检索数据并存储在本地变量中:Android
【发布时间】:2016-08-29 17:21:42
【问题描述】:

我的应用程序将用户位置数据存储在 Firebase Realtime Database 中。我打算做的是检索一些数据并在我的应用程序中处理它。我能够从 Firebase DB 获取所需的数据(通过调试检查)。现在我想将数据存储在局部变量中。我能够存储除一个值之外的所有值(调试器显示它的值为空,尽管它存在于datasnapshot.)。 Firebase 中的数据采用UserDataLocation 对象形式,以下是相同的类:

UserDataLocation类:

public class UserLocationData {

    String mobile, name, latitude, longitude, proximity, connection_limit, last_updated;

    public UserLocationData() {
    }

    public UserLocationData(String mobile, String name, String latitude, String longitude, String proximity, String connection_limit, String last_updated) {

        this.mobile = mobile;
        this.name = name;
        this.latitude = latitude;
        this.longitude = longitude;
        this.proximity = proximity;
        this.connection_limit = connection_limit;
        this.last_updated = last_updated;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getProximity() {
        return proximity;
    }

    public void setProximity(String proximity) {
        this.proximity = proximity;
    }

    public String getConnection_limit() {
        return connection_limit;
    }

    public void setConnection_limit(String connection_limit) {
        this.connection_limit = connection_limit;
    }

    public String getLast_updated() {
        return last_updated;
    }

    public void setLast_updated(String last_updated) {
        this.last_updated = last_updated;
    }
}

这是我检索和存储数据的方式:

                   @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        for (DataSnapshot child : dataSnapshot.getChildren()) {

                            userLocationData = child.getValue(UserLocationData.class);
                            String connectionKey = child.getKey();

                            if (!connectionKey.equals(currentUserMobile)) {

                                for (int count = 0; count <= Integer.parseInt(connections_limit); count++) {

                                    **String last_updated = userLocationData.last_updated;** //this is showing null in debugger

                                    Location connectionLocation = getLocationFromLatLng(userLocationData.latitude, userLocationData.longitude);

                                  {......}
                              }
                            }
                        }
                    }
                }

String last_updated = userLocationData.last_updated; 之外的所有变量都具有价值。

谁能帮我解决这个问题???

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    我已经解决了我的问题。这个错误很小但很重要。实际上,我使用last updated 作为存储上次更新时间的键。问题出在此密钥的格式上。它应该是 last_updated 或其他东西,但似乎 firebase DB 认为带有 spaces 的密钥无效。所以,我把我的密钥改成了last_updated,问题就解决了。

    希望它可以帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多