使用Firebase for Android更新特定值

时间:2015-10-24 06:44:42

标签: android firebase

我创建了如下的Firebase数据

tasks
-K1NRz9l5PU_0R8ltgXz
   Description: "test1"
   Status: "PENDING"
 -K1NRz9l5PU_0CFDtgXz
   Description: "test2"
   Status: "PENDING"

我需要将第二个对象的状态从PENDING更新为COMPLETED。我正在使用updateChildren方法,但它为任务子项添加了一个新节点。

如何在不创建新节点的情况下更新第二个节点的状态?

这是我现在的代码,

//按钮单击侦听器

  {

    Firebase m_objFireBaseRef = new Firebase(AppConstants.FIREBASE_URL);        
    final Firebase objRef = m_objFireBaseRef.child("tasks");    
    Map<String,Object> taskMap = new HashMap<String,Object>();
    taskMap.put("Status", "COMPLETED");
    objRef.updateChildren(taskMap); //should I use setValue()...?

 });

5 个答案:

答案 0 :(得分:35)

您没有指定要更新的任务的任务ID。

String taskId = "-K1NRz9l5PU_0CFDtgXz";

Firebase m_objFireBaseRef = new Firebase(AppConstants.FIREBASE_URL);        
Firebase objRef = m_objFireBaseRef.child("tasks");
Firebase taskRef = objRef.child(taskId);
Map<String,Object> taskMap = new HashMap<String,Object>();
taskMap.put("Status", "COMPLETED");
taskRef.updateChildren(taskMap);

或者,您只需在要更新的媒体资源上调用setValue()

即可
String taskId = "-K1NRz9l5PU_0CFDtgXz";

Firebase m_objFireBaseRef = new Firebase(AppConstants.FIREBASE_URL);        
Firebase objRef = m_objFireBaseRef.child("tasks");
Firebase taskRef = objRef.child(taskId);
Firebase statusRef = taskRef.child("Status");
statusRef.setValue("COMPLETED");

或者:

Firebase m_objFireBaseRef = new Firebase(AppConstants.FIREBASE_URL);        
Firebase objRef = m_objFireBaseRef.child("tasks");
objRef.child(taskId).child("Status").setValue("COMPLETED");

更新

不确定“我需要根据状态跟踪ID”是指。但是,如果要同步状态为Pending的所有任务,则执行以下操作:

Firebase m_objFireBaseRef = new Firebase(AppConstants.FIREBASE_URL);        
Firebase objRef = m_objFireBaseRef.child("tasks");
Query pendingTasks = objRef.orderByChild("Status").equalTo("PENDING");
pendingTasks.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot tasksSnapshot) {
        for (DataSnapshot snapshot: tasksSnapshot.getChildren()) {
            snapshot.getRef().child("Status").setValue("COMPLETED");
        }
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

答案 1 :(得分:5)

此代码将找到节点内容test2,然后更新状态。希望这有帮助。我正在使用Google Firebase。

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    final DatabaseReference reference = firebaseDatabase.getReference();
    Query query = reference.child("tasks").orderByChild("Description").equalTo("test2");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
            String key = nodeDataSnapshot.getKey(); // this key is `K1NRz9l5PU_0CFDtgXz`
            String path = "/" + dataSnapshot.getKey() + "/" + key;
            HashMap<String, Object> result = new HashMap<>();
            result.put("Status", "COMPLETED");
            reference.child(path).updateChildren(result);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Logger.error(TAG, ">>> Error:" + "find onCancelled:" + databaseError);

        }
    });

答案 2 :(得分:5)

您可以使用以下代码进行更新

HashMap<String, Object> result = new HashMap<>();
        result.put("Description", "Your Description comes here");

FirebaseDatabase.getInstance().getReference().child("tasks").child("-K1NRz9l5PU_0R8ltgXz").updateChildren(result);

它会更新特定的孩子

答案 3 :(得分:2)

you guyz没有帮助......我们正在努力了解哪些功能可以帮助我们获取密钥,以便在不比较任何值的情况下自动更新。如果有路径我会告诉你PERTH

Log.e(TAG, "Key2 is "+getRef(position).getKey());

请使用firebase Ui似乎原来的firebase还没有。

答案 4 :(得分:0)

您只需要从推送方法中获取正确的键值然后在addOnCompleteListener完成任务时更新子项

MyTaskModel myTaskModel = new MyTaskModel();
myTaskModel.Description = "My Text Is Here";
myTaskModel.Status = "PENDING";
        final FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
        final DatabaseReference databaseReference = mFirebaseDatabase.getReference().child("tasks");
        final DatabaseReference dbr= databaseReference.push();
        final String key = dbr.getKey();
        Task task1 = dbr.setValue(myTaskModel);
        task1.addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                                     Map<String, Object> taskMap = new HashMap<String, Object>();
                                     taskMap.put("Status", "COMPLETED");                  
                                     FirebaseDatabase.getInstance().getReference().child("tasks").child(key).updateChildren(taskMap);

                                    }
                                }
                            );