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;
}
}