Firebase节点更新

时间:2017-11-29 12:16:11

标签: android firebase firebase-realtime-database

我需要更新节点的单个字段,即通知0/1,其中'schedule_id = 1'。我已经尝试了很多程序但是无法更新或有时。

我也尝试过这个解决方案,但失败了(Update and delete data in Firebase)这是添加一个新节点

Firebase结构:

 {
   "schedule":{
      "21-11-2017":{
         "route":{
            "1":{
               "kid":{
                  "21":{
                     "-KzDdfZtds7rxwETF4oN":{
                        "color":"#B71C1C",
                        "delay_time":"45 mins delay",
                        "is_active":"0",
                        "msg":"Driver out of garage",
                        "notify":"dsds",
                        "real_time":"10:30 PM",
                        "schedule_id":"1",
                        "schedule_time":"5:00"
                     }
                  }
               }
            }
         }
      }
   }
}

我试过这段代码。 在这种情况下,问题发生就像调试器没有进入'query.addListenerForSingleValueEvent`

private void updatefirebsenode() {
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        final DatabaseReference reference = firebaseDatabase.getReference();
        Query query = reference.child("schedule").child("21-11-2017").child("route").child("1").child("kid").child("21").orderByChild("schedule_id").equalTo("1");
///from this line debugger is exiting 
        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("notify", "1");
                reference.child(path).updateChildren(result);
            }

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

安排POJO课程

public class Schedule {
    private String delay_time;
    private String is_active;
    private String real_time;
    private String schedule_id;
    private String schedule_time;
    private String color;
    private String noti_message;
    private String notify;
    private transient Context context;


    public Schedule(String notify) {
        this.notify = notify;
    }

    public Schedule() {
    }

    public String getDelay_time() {
        return delay_time;
    }

    public void setDelay_time(String delay_time) {
        this.delay_time = delay_time;
    }

    public String getIs_active() {
        return is_active;
    }

    public void setIs_active(String is_active) {
        this.is_active = is_active;
    }

    public String getReal_time() {
        return real_time;
    }

    public void setReal_time(String real_time) {
        this.real_time = real_time;
    }

    public String getSchedule_id() {
        return schedule_id;
    }

    public void setSchedule_id(String schedule_id) {
        this.schedule_id = schedule_id;
    }

    public String getSchedule_time() {
        return schedule_time;
    }

    public void setSchedule_time(String schedule_time) {
        this.schedule_time = schedule_time;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getNoti_message() {
        return noti_message;
    }

    public void setNoti_message(String noti_message) {
        this.noti_message = noti_message;
    }

    public String getNotify() {
        return notify;
    }

    public void setNotify(String notify) {
        this.notify = notify;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用setValue方法设置schedule_id的值,而不是使用updateChildren,但首先您必须引用schedule_id节点。

   final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("schedule").child("21-11-2017").child("route").child("1").child("kid").child("21").orderByChild("schedule_id");
                        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String schedule_id =(String )dataSnapshot.getValue();
                                                                    mDatabase.setValue("setDesiredValueTo Schedule_id");
                                Log.wtf("MYLOG", "onDataChange: schedule_id updated" );
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

我更新用户点的示例:

final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("Users").child(created_by_referalUsername).child("pointsTotal");
                        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String points =(String )dataSnapshot.getValue();
                                int i  =Integer.valueOf(points);
                                i +=250;
                                mDatabase.setValue(i);
                                Log.wtf("MYLOG", "onDataChange: points added to referal owner profile" );
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

这可能会对你有帮助。

答案 1 :(得分:0)

当您使用getChildren()方法时,这意味着您正在迭代dataSnapshot对象,这意味着您将获得所有密钥,而不仅仅是一个。为了能够使用push()方法生成的特定密钥,您需要使用以下代码行将其首先存储到字段中:

String key = yourRef.push().getKey();

因此,当您推送新密钥时,是您可以存储它的时刻。

为了更新Firebase数据库中字段的值,您不需要附加监听器,您只能直接在引用上使用setValue()方法,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("schedule")
    .child("21-11-2017")
    .child("route")
    .child("kid")
    .child("21")
    .child("-KzDdfZtds7rxwETF4oN")
    .child("notify")
    .setValue("1");