Boris Oortwijn
Boris Oortwijn

Reputation: 3

FirebaseDatabase only saving the id

My Firebase Database is only saving the ID of my object, and not the attributes.

Firebase is connected with my android app and can save and retreat data from the database, when i try to add data to the database, the object in firebase is created, but after that the attributes that i want to add to this object in the database are not saved.

Can anyone help me?

My database class:

public class Database
{
   private static Database database = null;

   private static FirebaseDatabase firebaseDatabase;

   private static DatabaseReference tractorRef;

   private Database()
   {
       firebaseDatabase = FirebaseDatabase.getInstance();

       tractorRef = firebaseDatabase.getReference("tractor");
   }

   public static void setInstance()
   {
       if(database == null)
       {
           database = new Database();
       }
   }

   public static DatabaseReference getTractorRef()
   {
    setInstance();
    return tractorRef;
   }
} 

User class that i try to save:

import android.util.Log;

public class User
{
    private String id;
    private String name;
    private int age;

    public User(String id, String name, int age)
    {
        this.id = id;
        this.name = name;
        this.age = age;

        Log.d("User: ID:", "" + this.id);
        Log.d("User: NAME:", "" + this.name);
        Log.d("User: AGE:", "" + this.age);

    }

    public String getId() {
        return id;
    }
}

The code that i use to add the object in firebase:

User user = new User(Database.getTractorRef().push().getKey(), "Someone", 21);
Database.getTractorRef().child(user.getId()).setValue(user);

The output in my Log.d():

......D/User: ID:: -LPD2UbA4sVqPXxWFv0V

......D/User: NAME:: Someone

......D/User: AGE:: 21

What i see in Firebase Database:

Output from Firebase Database

Upvotes: 0

Views: 37

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

It's only saving the id because getId() the only public getter method you've defined. If you want to save the other fields, make public getters for the other fields as well.

Upvotes: 1

Related Questions