android:无法更新Firebase数据库中的值

时间:2018-08-05 18:55:19

标签: android firebase-realtime-database

使用以下代码,我有了“历史节点”。

历史节点

 {
  "History" : {
    "-LJANnl9ofbXlxLGxTGg" : {
      "destination" : "3 Paradise Row",
      "driver" : "ReGqRl2IIUhmLIqdBqBIELHBsgE3",
      "location" : "1 Union St",
      "payment response" : "approved",
      "rating" : 0,
      "ridePrice" : 5.25,
      "rider" : "l3PPyBQux7YJhGGTdexxwDBteLM2",
      "riderPaid" : "true",
      "status" : "accepted",
      "timestamp" : 1533494377
    }
  }

使用以下代码,我试图将“状态”值更新为从“已接受”更新为“ arrived_loc”。运行代码时,firebase中有黄色突出显示,但状态值保持“已接受”。

将状态更新为“ arrived_loc”

driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRefchild("History").orderByChild("driver").equalTo(driverId);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            DataSnapshot nodeDS = dataSnapshot.getChildren().iterator().next();
            String key = nodeDS.getKey();
            Log.e(TAG, "key = " + key);
            String path = "/" + dataSnapshot.getKey() + "/" + key;
            Log.e(TAG, "path = " + path);

            HashMap<String, Object> update = new HashMap<>();
            update.put("status", "arrived_loc");
            rootRef.child(path).updateChildren(update);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

对于我做错了任何事情,我们将不胜感激。

编辑#1

如果我运行以下代码:

DatabaseReference arrivedLoc = FirebaseDatabase.getInstance().getReference("History");
    String historyId = arrivedLoc.push().getKey();
    arrivedLoc.child(historyId).child("status").setValue("arrived at pickup");

我得到一个额外的历史记录节点,并且没有更新:

"History" : {
    "-LJAVN2iAWfRREdlBevb" : {
      "destination" : "3 Union St",
      "driver" : "ReGqRl2IIUhmLIqdBqBIELHBsgE3",
      "location" : "123 Main Street",
      "payment response" : "approved",
      "rating" : 0,
      "ridePrice" : 5.38,
      "rider" : "l3PPyBQux7YJhGGTdexxwDBteLM2",
      "riderPaid" : "true",
      "status" : "accepted",
      "timestamp" : 1533496391
    },
    "-LJAVaFd3Gzq3iIZGHeP" : {
      "payment response" : "approved",
      "riderPaid" : "true",
      "status" : "accepted"
    }
  }

编辑#2-数据库图像已冻结

enter image description here

编辑#3

由于某种原因,我似乎无法使其正常工作,因此我将“ status”重命名为“ requestStatus”,并创建了一个名为rideStatus的新变量,例如:

status = "arrived at pickup";

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("History");
    rootRef.child(requestId).child("rideStatus").setValue(status);

这似乎暂时可行,但是当我需要更改rideStatus的值时,希望它会起作用。

2 个答案:

答案 0 :(得分:1)

尝试使用不带push()函数的代码:

DatabaseReference arrivedLoc = FirebaseDatabase.getInstance().getReference("History");
    String historyId = arrivedLoc.getKey();
    arrivedLoc.child(historyId).child("status").setValue("arrived at pickup");

(取决于您需要在应用上实现的功能,可能会有所不同)


更新#2:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("History");
            addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.exists()){

                        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                            snapshot.getRef().child("status").setValue("arrived at pickup");
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

答案 1 :(得分:0)

尝试使用updateChildren()

driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();
HashMap<String, String> newData = new HashMap<>();
newData.put("status", "arrived_loc");
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("History/" + driverId); // Or however the path is
rootRef.updateChildren(newData);