Reputation: 15
I am trying to update a specific child Value in Firebase, with the help of below code. But instead of getting updated, the existing child is getting deleted.
I have gone through similar kind of questions, nothing worked for me. Any help would be highly appreciated.
please find code which i tried to update and also find the firebase screenshot.
reffortask=FirebaseDatabase.getInstance().getReference()
.child("taskmanagement").child("new");
timeStamp=getIntent().getStringExtra("tTimeStampTask");
userName= getIntent().getStringExtra("tUserName");
String updatet =taskUpdate.getText().toString().trim();
if (userName!=null) {
updatedby = "Updated by " + userName + " on " + currentDate + ":- " + updatet;
}
private void updateTask(){
HashMap<String,Object> updateTask = new HashMap<>();
updateTask.put("remarksTask",updatedby);
if (timeStamp!=null){
reffortask.orderByChild("timeStampTasK").equalTo(timeStamp)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DataSnapshot childTask = dataSnapshot.getChildren().iterator().next();
String taskKey=childTask.getKey();
final HashMap<String,Object> updateTask = new HashMap<>();
updateTask.put("remarksTask",updatedby);
String last =""+childTask.child("remarksTask").getValue();
lastUpdate.setText(last);
reffortask.child(taskKey).updateChildren(updateTask)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(EditTask.this,"Remarks Updated ",Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 0
Views: 73
Reputation: 598847
If a value gets deleted that means that either you're calling removeValue()
on that property, or you are write null
to it. Given the code you shared, the latter seems most likely.
So it seems like updatedby
is null
in this code:
updateTask.put("remarksTask",updatedby);
You might want to verify this by logging the value right before this line of code, or protect against this by putting an `if (updatedby != null) around it.
Unrelated: your code seems a bit overly complex, and non-idiomatic. I'd write the same functionality as:
reffortask.orderByChild("timeStampTasK").equalTo(timeStamp)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DataSnapshot childTask = dataSnapshot.getChildren().iterator().next();
DataSnapshot childRemark = childTask.child("remarksTask");
String last = childRemark.getValue(String.value);
lastUpdate.setText(last);
childRemark.getReference().setValue(updatedby).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(EditTask.this,"Remarks Updated ",Toast.LENGTH_SHORT).show();
}
});
}
...
The differences:
onDataChange
handles only one result anyway.remarksTask
with getValue(String.value)
. This will give a better error message if you ever have the wrong type in the JSON.childRemark.getReference().setValue(updatedby);
, instead of building a map with only a single value in it.Upvotes: 1