【问题标题】:Firebase Set key of the root Id根 Id 的 Firebase 设置键
【发布时间】:2016-05-20 20:37:21
【问题描述】:

我只是想知道如何使用我自己的键而不是在顶部的 while 循环中生成的 Push() 键来设置 root id..

  -Waypoint  
    -RootID <-- This key
      -Points

........

 LinkedHashMap<String, Object> pointHolder = new LinkedHashMap<>();
 LinkedHashMap<String, Object> waypointHolder = new LinkedHashMap<>();

 waypointsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/waypoints";
        mainRef = new Firebase(waypointsUrl);

..................

更新代码

            while (isFirstTime) {
                // Generates root id for date
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date();
                mLastUpdateTime = dateFormat.format(date);

                // Generates root id
                 mainRefKey = mainRef.push();

                isFirstTime = false;
            }


            // Generates new id for point
            Firebase wayRef1 = mainRefKey.push();
            String pointKey = wayRef1.getKey();


            // Create Waypoint > Point >  put lat and lon
            LinkedHashMap<String, Object> latLng = new LinkedHashMap<String, Object>();
            latLng.put("latitude", 123);
            latLng.put("longitude", 123);


            // Puts lat and lon  ** under a Point Key **
            pointHolder.put(pointKey, latLng);


            // Waypoint > POINT (contains multiple points)
            waypointHolder.put("points", pointHolder);
            waypointHolder.put("timeStamp", "24/3-2016");
            waypointHolder.put("travelType", "travel");


            // ROOT ID > Waypoint
            mainRefKey.updateChildren(waypointHolder);
        }
    });

所需结构的图片:

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    我建议查看docs on saving data 并阅读api docs

    更改循环以执行以下操作:

    while (isFirstTime) {
      // Generates root id for date
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date = new Date();
      mLastUpdateTime = dateFormat.format(date);
    
      // Generates root id
      mainRefKey = mainRef.child(mLastUpdateTime);
    
    
      isFirstTime = false;
    }
    

    【讨论】:

    • 不起作用。 someIdYouWant 是什么。猜猜它是根 id,但当前的 id 是什么?
    • 这样做是为了让它工作:mainRef.child(mLastUpdateTime).setValue(waypointHolder); 在底部。
    • 我现在看到它添加了一个推送的密钥和我自己的密钥,其中推送的密钥始终相同并被覆盖
    • 但是您发布的代码将关键点添加到了关键点,但我需要将其添加到保留点的关键点
    • 添加了所需解决方案的图片。但是 atm 它添加 -KICxoNHLX0BkonnPcNY 作为键而不是日期。
    猜你喜欢
    • 2011-10-24
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多