Updating the data on Firebase Android

2020-03-01 17:15发布

I want to update the date on Firebase on a specific node.

DB Structure:

enter image description here

I am trying as

private void updateData() {
database = FirebaseDatabase.getInstance();
myref = database.getReference();
myref.child("myDb").child("awais@gmailcom").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        dataSnapshot.getRef().child("leftSpace").setValue(newValue);
        dialog.dismiss();

    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d("User", databaseError.getMessage());
    }
});
}

I want to update the leftSpace key with the value of newValue, newValue is the type of string here. But it is not updating the value in Firebase.

If I give here

dataSnapshot.getRef().child("leftSpace").setValue(765);

it updates well. But I want to update in the format of string on the Firebase.

I saved the data on Firebase of all string types. (My pattern class contains all of the type strings)

Why it is not updating the newvalue of type string here?

Edit 1 Suggested by @Rjz Satvara

You method is adding a new node under myDB as

enter image description here

It is not updating the already one.

3条回答
Root(大扎)
2楼-- · 2020-03-01 17:47

you can assign new value in same child,like

  Firebase firebase = new Firebase("your database link/myDb");
    firebase.child("awais@gmail.com").child("leftSpace").setValue("newValue");
查看更多
唯我独甜
3楼-- · 2020-03-01 17:54

According to Firebase Official Documentation you can update the specific node of parent node in this way

Using setValue() in this way overwrites data at the specified location, including any child nodes. However, you can still update a child without rewriting the entire object. If you want to allow users to update their profiles you could update the username as follows:

    FirebaseDatabase  database = FirebaseDatabase.getInstance();
    DatabaseReference mDatabaseRef = database.getReference();

    mDatabaseRef.child("TABLE_NAME").child("orderStatus").setValue(2);

Note! TABLE_NAME mean your parent node whose child node you want to update.

enter image description here

查看更多
来,给爷笑一个
4楼-- · 2020-03-01 18:08

In Firebase To update specific value you can use it...

ref.child("myDb").child("awais@gmailcom").child("leftSpace").setValue("YourDateHere");

or you can move into child using "/" as follow :

ref.child("myDb/awais@gmailcom/leftSpace").setValue("YourDateHere");
查看更多
登录 后发表回答