Animesh Jena
Animesh Jena

Reputation: 1551

Recreate a firebase object by copying some data android

I am implementing firebase in my app. I have a requirement where I have to change the key value of one object. Please refer the below image as a reference to my firebase database.

enter image description here

For the DCu1, I need to change its key value to something else, suppose DCu4. Now for that I am creating another cloned object of DCu1 with changed key value as DCu4 and after which I will delete DCu1. The issue is with the nested parameters like DeviceList, Status, Request. How can I copy these values to the new cloned object? need your help. Thanks in advance.

Upvotes: 3

Views: 403

Answers (1)

Prince Odame
Prince Odame

Reputation: 544

When you read data at a node, all the nested stuff are downloaded.

You can use the code below:

 FirebaseDatabase.getInstance().getReference().child("/path/to/DCu1").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            FirebaseDatabase.getInstance().getReference().child("path/to/DCu4").setValue(dataSnapshot.getValue());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I hope this helps

Upvotes: 2

Related Questions