2016-04-27 48 views
1

如何將push()添加到航點的孩子?不能得到它的權利..Android與Firebase,如何推動兒童數據?

Map mWaypoints = new HashMap(); 
    mWaypoints.put("latitude", mCurrentLatlngOnCreate.latitude); 
    mWaypoints.put("longitude", mCurrentLatlngOnCreate.longitude); 

    Map mParent = new HashMap(); 
    mParent.put("routeID", "my route id"); 
    mParent.put("routeName", "my route name"); 
    mParent.put("Waypoints", mWaypoints); 

    myFirebaseRef.push().setValue(mParent); 

這裏是當前的結果,但希望有下面的推送ID Waypoints。

-KGNWnjgXPC0kHYiOKG7 
     Waypoints  // <--- Want to get the unique below here 
      latitude: 31 
      longitude: 421 
     routeID: "my route id" 
     routeName: "my route name" 

回答

7

push()是一個純粹的客戶端操作,您可以單獨調用。所以:

Map mWaypoints = new HashMap(); 
mWaypoints.put("latitude", mCurrentLatlngOnCreate.latitude); 
mWaypoints.put("longitude", mCurrentLatlngOnCreate.longitude); 

String key = myFirebaseRef.push().getKey(); 
Map mWayPointsMap = new HashMap(); 
mWayPointsMap.put(key, mWayPoints); 

Map mParent = new HashMap(); 
mParent.put("routeID", "my route id"); 
mParent.put("routeName", "my route name"); 
mParent.put("Waypoints", mWayPointsMap); 

myFirebaseRef.push().setValue(mParent); 
0
ref.push().getkey(); 

使用此代碼爲拿到鑰匙的推操作,然後

ref.yourkey.setValue("Your value"); 

感謝