Musthafa
Musthafa

Reputation: 15

Android Studio - Firebase existing child is getting deleted, when try to update the value

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

enter image description here

Upvotes: 0

Views: 73

Answers (1)

Frank van Puffelen
Frank van Puffelen

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:

  • Limit the query to at most one result, since your onDataChange handles only one result anyway.
  • Get the current value of remarksTask with getValue(String.value). This will give a better error message if you ever have the wrong type in the JSON.
  • Set the value of the property directly with childRemark.getReference().setValue(updatedby);, instead of building a map with only a single value in it.

Upvotes: 1

Related Questions