是否会自动添加其他firebase数据库字段?

时间:2017-11-17 21:33:38

标签: android firebase firebase-realtime-database

我有一本书课

public class Books {

    public String bookName;
    public String author;
    public String copies;

    public Books() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public Books(String name, String author, String copies) {
        this.bookName = name;
        this.author = author;
        this.copies = copies;
    }

    public String getBookName() {
        return bookName;
    }

    public String getAuthorName() {
        return author;
    }


    public String getCopies() {
        return copies;
    }

}

我正在添加firebase实时云数据库

mDatabase = FirebaseDatabase.getInstance("https://cmpe-2b339.firebaseio.com").getReference();

Books book = new Books(bookName,author,copies);
                    mDatabase.push().setValue(book);

然而在firebase concole中,它的出现

{
  "-KzAt3eHDBtD7gFvLIbn" : {
    "author" : "J",
    "authorName" : "J",
    "bookName" : "Hi",
    "copies" : "5"
  }
}

添加了作者字段,我不明白

2 个答案:

答案 0 :(得分:1)

您有一个名为“author”的公共变量和一个名为“getAuthorName”的公共getter。 Firebase反射用于在数据库中创建数据。修复:将变量设为私有并保留getter,然后添加setter。

答案 1 :(得分:1)

用此

替换您的课程
public class Books {
private String bookName;
private String author;
private String copies;

public Books () {

}

public Books(String bookName, String author, String copies) {
    this.bookName = bookName;
    this.author = author;
    this.copies = copies;
}

public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getCopies() {
    return copies;
}

public void setCopies(String copies) {
    this.copies = copies;
}
}