rainman rainman - 2 months ago 30
Android Question

Write new Data in Android Firebase Database

I am using Firebase Database in an Android application, and everytime a user is registred I store some values in the database for this purpose I am doing what follows:

database = FirebaseDatabase.getInstance();
DatabaseReference mRef = database.getReference().child("Users").push();
FirebaseUser user = mAuth.getCurrentUser();
String userId = user.getUid();
mRef.child("User").child("age").setValue(selectedAge);
mRef.child("User").child("gender").setValue(finalGender);
mRef.child("User").child("userId").setValue(userId);
mRef.child("User").child("username").setValue(username);


but when I go to my firebase console I see that a kind of id is automaticaly generated which I do not need since as you can see in the image I set my own id.

enter image description here

Is there a way to avoid this, and create nodes like the second User node in the image ??

NOTE: the second user node i added it manualy

Answer

To directly answer your question here is the change I'd suggest:

FirebaseDatabase database =  FirebaseDatabase.getInstance();
FirebaseUser user =  mAuth.getCurrentUser();
String userId = user.getUid();
DatabaseReference mRef =  database.getReference().child("Users").child(userId);
mRef.child("age").setValue(selectedAge);
mRef.child("gender").setValue(finalGender);
mRef.child("username").setValue(username);

I'd also suggest that you create a Java class to represent whatever user data you want to store in your database. Taking advantage of Firebase's ability to read/write to your database using Java classes will make those operations much easier. For example you add a User class to your project like the following:

public class User {

    public double age;
    public String gender;
    public String userName;

    //required default constructor
    public User() {
    }

    public User(double age, String gender, String userName) {
        this.age = age;
        this.gender = gender;
        this.userName = userName;
    }

    public double getAge() {
        return age;
    }

    public void setAge(double age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

By using a java class to write to your database you can update your write operation like so:

FirebaseDatabase database =  FirebaseDatabase.getInstance(); 
FirebaseUser user =  mAuth.getCurrentUser();
String userId = user.getUid();
User user = new User(26, "M", "user001");
DatabaseReference mRef =  database.getReference().child("Users").child(userId);
mRef.setValue(user);

Notice there is no field to the store user's uid in the Java object. This is up to you but I did not include it for two reasons. First, in the database we are using the user's uid as the key to store there data in a unique location. Second, since you are using Firebase Authentication, in your app if you need to get the id of the currently signed in user you can access it via FirebaseAuth.