1

I have a relative simple Firebase database which looks like this:

enter image description here

How can i update the Room1 node? If i use this code, in stead of updating that node, it adds another one, with the new name, Room2.

databaseReference = FirebaseDatabase.getInstance().getReference().child("Rooms");
Query updateQuery = databaseReference.child(Room1).orderByKey().equalTo(Room1);
updateQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        dataSnapshot.getRef().getParent().child("Room2").setValue("");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

Thanks in advance!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    `FirebaseDatabase.getInstance().getReference().child("Rooms1").setValue("your value here");` will be enough. Why you add all that code? Did you also want to detect whenever data changed? – koceeng Jan 19 '17 at 00:54
  • All i want to do, is to change the name of `Room1` to `Room2`. If i do as you said, `Room1` gets a value like this: `"Room1": "Room2"`. How can i update only `Room1`? – Alex Mamo Jan 19 '17 at 01:08
  • Oh, rename? did you have `Room` custom object? if you have then it would be more easy. The concept is get all the data from `Room1`, and assign them to `Room` custom object. Then you remove `Room1` node from database. Then add new node called `Room2` and assign `Room` custom object into that new node. – koceeng Jan 19 '17 at 01:57
  • Can you please write me some lines of code? Thanks! – Alex Mamo Jan 19 '17 at 02:03
  • honestly, it's a bit difficult to make that `Room` custom object (and my solution need that). Let me ask you, did `Room1` have child node other than `-Kah0vRP...`? if it only contain that as single node, I recomend you change your database structure – koceeng Jan 19 '17 at 02:17
  • Yes, there are a lot of other child nodes like `-Kah0vRP...`. All i want to do, is to change the name of `Room1` in `Room2`. Is there another possibility to achieve this? Even if i need to change the database structure. – Alex Mamo Jan 19 '17 at 02:30
  • 2
    You cannot change keys of an existing node or move branches in the Firebase Database. There isn't an API for this. You'll need to create a copy and then delete the original. This has been asked before, so I'll find a duplicate. – Frank van Puffelen Jan 19 '17 at 04:32
  • 1
    I did not find that post, but thanks, it solved my problem. Creating a copy and than deleting the original is the right answer. – Alex Mamo Jan 19 '17 at 10:35

1 Answers1

2
databaseReference = FirebaseDatabase.getInstance().getReference().child("Rooms").child(Room1);
databaseReference.setValue()

with whatever new value you want in the setValue() parentheses

Pat Myron
  • 4,437
  • 2
  • 20
  • 39
  • Your answer is the same as koceeng's. It has the same result. All i want to do, is to change the name of Room1 to Room2. If i do as you said, Room1 gets a value like this: "Room1": "Room2". How can i update only Room1? – Alex Mamo Jan 19 '17 at 01:12