I am currently writing a part of an application where a user scans a QR code which holds the value of the user UID. Upon scanning this, I want to create a new key in my Firebase Realtime database containing the information related to the user with that UID called userInformation. Below is a an example of the format I wish the values to be stored as:
Currently it is being stored without the userInfromations key, it is only being stored with the UID, like so:
Here is the related code:
case FirebaseVisionBarcode.TYPE_TEXT:
{
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
userInformations = new ArrayList<>();
DatabaseReference username = ref.child(item.getRawValue()).child("username");
DatabaseReference dateOfBirth = ref.child(item.getRawValue()).child("dateOfBirth");
username.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String username = dataSnapshot.getValue(String.class);
//Toast.makeText(getApplicationContext(),username, Toast.LENGTH_LONG).show();
//it will create a unique id and we will use it as the Primary Key for our UserInfo
String id1 = ref.push().getKey();
//creating an UserInfo Object
UserInformation userInformation = new UserInformation(id1, username);
//Saving the Artist
ref.child(id1).setValue(userInformation);
//displaying a success toast
Toast.makeText(getApplicationContext(), username + " has been added", Toast.LENGTH_LONG).show();
}
I realize also that by changing:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
to:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("userInformations");
I am able to get the key named userInformations, but it no longer saves the value for the userInformationName like below:
At the moment I am just unsure how to structure my code to get both of these things.
According to the last comemnts, to achieve a schema that looks like this:
rootRef
|
--- userInformations
|
--- pushId
|
--- "userInformationId: "-LXO5 ... H-o3-P"
|
--- "userInformationName: "[email protected]"
Please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userInformationsRef = rootRef.child("userInformations");
String userInformationId = userInformationsRef.push().getKey();
String userInformationName = "[email protected]";
UserInformation userInformation = new UserInformation(userInformationId, userInformationName);
userInformationsRef.child(userInformationId).setValue(userInformation);