Reputation: 2047
I already have a onDataChange written to get values from a different child node,but what I need is to change the key to let's say "new" and then retrieve the data from that child
Any inputs would be appreciated ..
Upvotes: 0
Views: 238
Reputation: 712
As far as i understand, you want to change a field and bring back the changed data.
for e.g, for this data structure, if you want the change Username
, create a User modal with different name, User newUser = new User("https://...","newUserName)
. And change the field in User modal
public String Profile;
public String newKey;
Then give the new modal to that uid,
FirebaseDatabase.getInstance().getReference().child("Users").child("ECWHIksxJ0Q5SUIIrev4BjnjmrJ3").setValue(newUser, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
DatabaseReference yourRef = databaseReference.getRef();
}
});
Now you have the changed data's reference. Did you try something like this?
Update:
If you want to update multiple value/field with single action, you should create a map and put every path into it with values.
In firebase, every table, every specific field is an path. Consider the data structure shown above. If you want change Username
field:
HashMap<String, Object> updateMap = new HashMap<>();
updateMap.put("/Users/ECWHIksxJ0Q5SUIIrev4BjnjmrJ3/Username", "newUserName");
For different user:
updateMap.put("/Users/DIFFERENT_USER_KEY/Username", "newUserName2");
For diffrent table:
updateMap.put("/YourOtherTable/DifferentPath/DifferentField", true);
Then get your database root reference and update all at once.
FirebaseDatabase.getInstance().getReference().updateChildren(updateMap).addOnCompleteListener(new OnCompleteListener...)
This is very simple way to update a lot of things in one action, but it's causing problems when writing firebase rules
. Because you get your root reference and try to set a value to it. The choice is yours.
Upvotes: 1