2016-08-29 214 views
0

我的應用程序將用戶位置數據存儲在Firebase Realtime Database中。我打算做的是檢索一些數據並在我的應用程序中處理它。我可以從Firebase DB中獲取所需的數據(通過調試進行檢查)。現在我想將數據存儲在局部變量中。我能夠存儲除一個值之外的所有值(調試器顯示它的值爲空,儘管它存在於datasnapshot中。)。從火力地堡數據是UserDataLocation對象的形式和下面是相同的類:從Firebase數據庫中檢索數據並存儲在本地變量中:Android

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;之外的所有變量都具有價值。

任何人都可以幫我解決這個問題嗎?

回答

3

我解決了我的問題。這個錯誤很小但很重要。實際上,我使用last updated作爲存儲上次更新時間的關鍵。問題在於這個鍵的格式。它應該是last_updated或其他東西,但似乎firebase數據庫認爲密鑰spaces無效。所以,我將密鑰更改爲last_updated並解決了問題。

希望它可以幫助別人。

相關問題