问题
Click this to see Firebase Data Structure
I want to update my information (firstname, middlename, lastname) which is in a different location. I'm updating my information using student account. It only update under student but I don't know how to update it under teacher. If I update my first name, middle name, last name, I want my first name, middle name, and last name update also under the two section under the class. Please help me.
回答1:
To update a record in a Firebase database, you can use setValue() method directly on the reference. Assuming that teacher is the direct child of the Firebase root, you can use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference firstNameRef = rootRef.child("teacher").child(teacherId).child("class").child(classId).child("Listofstudents").child(listId).child("firstname");
firstNameRef.setValue("New Name");
In which teacherId, classId and listId are those random generated keys that i see in your database.
If you want to update the same data, in a different location, just add another reference to desired location and use the same setValue() method.
Hope it helps.
来源:https://stackoverflow.com/questions/44755198/how-can-i-update-my-information-in-different-location-android-firebase