2

I need to update a single field of the node i.e notify to 0/1 having 'schedule_id=1'.I have tried a lot of procedures but not able to update or sometimes.

I also have tried this solution but failed (Update and delete data in Firebase) this is adding a new node

Firebase structure:

 {
   "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"
                     }
                  }
               }
            }
         }
      }
   }
}

I have tried this code. In this case, problem is happening like the debugger is not getting inside '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);
            }
        });
    }

Schedule POJO class

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

Zafar Alam
  • 21
  • 3

2 Answers2

0

Instead of using updateChildren you can use setValue method to set the value of schedule_id but first of all you must have reference to schedule_id node.

   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) {

                            }
                        });

my Example to update user points:

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) {

                            }
                        });

this may help you.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
0

When you are using getChildren() method it means that you are iterating on the dataSnapshot object which in term means that you are getting all the keys and not only one. To be able to use a particular key that is generated by the push() method, you need to store it first into a field using this line of code:

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

So when you are pushing for a new key, is the moment in which you can store it.

In order to update a value of a field in a Firebase database, you don't need to attach a listener, you can only use setValue() method directly on the reference like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("schedule")
    .child("21-11-2017")
    .child("route")
    .child("kid")
    .child("21")
    .child("-KzDdfZtds7rxwETF4oN")
    .child("notify")
    .setValue("1");
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • the firebase node is created from the server / php end.My job is to update the notify node when my job is done in the android end – Zafar Alam Nov 29 '17 at 12:58
  • In this case, the php server should send you the key for which you must do the update. You cannot use a key in your `DatabaseReference` without knowing it and you cannot simply query your database for a key that you literally don't know it. – Alex Mamo Nov 29 '17 at 13:02