如何在firebase Live数据库中保存模型?

时间:2018-06-13 14:23:39

标签: android

我无法在数据库中保存数据, 我在网站上的指南做了一切,网站上的数据库没有更新,我做错了什么?

这是我的功能:

public void updateUser() {
        // Write a message to the database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("users");

        myRef.setValue(appUser);
    }

这是我的用户类:

公共类用户{

    public User () {

    }
    public User(String username, String password, String deviceId, String email, String gender, String lookingFor, String shortDescription, String longDescription, String city, String country, String phone, ArrayList<Integer> profilePictures, int defaultPicture, int birthYear, int birthMonth, int birthDay) {
        this.username = username;
        this.password = password;
        this.deviceId = deviceId;
        this.email = email;
        this.gender = gender;
        this.lookingFor = lookingFor;
        this.shortDescription = shortDescription;
        this.longDescription = longDescription;
        this.city = city;
        Country = country;
        this.phone = phone;
        this.profilePictures = profilePictures;
        this.defaultPicture = defaultPicture;
        this.birthYear = birthYear;
        this.birthMonth = birthMonth;
        this.birthDay = birthDay;
    }

    public String username;
    public String password;
    public String deviceId;
    public String email;
    public String gender;
    public String lookingFor;
    public String shortDescription;
    public String longDescription;
    public String city;
    public String Country;
    public String phone;
    public ArrayList<Integer> profilePictures;
    public int defaultPicture;
    public int birthYear;
    public int birthMonth;
    public int birthDay;

}

并且数据库表仍未更新:

enter image description here

我做错了什么?

谢谢

2 个答案:

答案 0 :(得分:2)

好像第一次尝试插入数据时,出现了一些问题。使用您将鼠标悬停在数据库名称上时的十字按钮删除以前的数据。

enter image description here

然后使用child()插入此类数据

public void updateUser() {
    // Write a message to the database
    FirebaseDatabase database = FirebaseDatabase.getInstance();

    //getReference().child() is important
    DatabaseReference myRef = database.getReference().child("users");

    //This will generate a new key. Sort of like the key of a list item
    String key = myRef.push().getKey();
    myRef.child(key).setValue(appUser);

    //Add a ValueEventListener to check whether the data you inserted worked or not
    myRef.child(key).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //Data inserted
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                //error
            }
        });
}

注意 - 同时检查您是否尝试插入appUser的空值。

答案 1 :(得分:0)

将updateUser()更改为以下内容:

public void updateUser() {
        // Write a message to the database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference();

        //Create a child reference
        DatabaseReference userRef = myRef.child("users");

        //pass the model value to the child reference 
        userRef.setValue(appUser);
    }