I have a Firebase data created as follows
tasks
-K1NRz9l5PU_0R8ltgXz
Description: \"test1\"
Status: \"PENDING\"
-K1NRz9l5PU_0CFDtgXz
Description: \
You're not specifying the task id of the task that you want to update.
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);
Alternatively, you can just call setValue()
on the property you want to update
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");
Or:
Firebase m_objFireBaseRef = new Firebase(AppConstants.FIREBASE_URL);
Firebase objRef = m_objFireBaseRef.child("tasks");
objRef.child(taskId).child("Status").setValue("COMPLETED");
Not sure what "I need to track the ID based on the status" means. But if you want to synchronize all tasks that are in status Pending
, you'd do:
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());
}
});
This code will find the node content test2
, then update the Status. Hope this help. I am using 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);
}
});
You guyz are not helping ...we are trying to know what function can help us get the key so that the updates can be automatically without comparing any value . Am on the quest if there is a path i will let u know PERTH
Log.e(TAG, "Key2 is "+getRef(position).getKey());
Please usefirebase Ui Seems the original firebase doesnt have yet.
You can use code below to update
HashMap<String, Object> result = new HashMap<>();
result.put("Description", "Your Description comes here");
FirebaseDatabase.getInstance().getReference().child("tasks").child("-K1NRz9l5PU_0R8ltgXz").updateChildren(result);
It will update a specific child
You Just need to get the right key Value from the push Method then Update the children when your task is completed by 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);
}
}
);