- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个类 User
具有属性 name
,age
,nationality
,eyeColor
我有三个构造函数。
每当我尝试使用构造函数 2 在 Firebase 中保存数据时,我发现数据实际上是使用构造函数 3 保存的,age
和 eyeColor
为 0
和 null
。
如何在不为构造函数 3 创建新类的情况下仅使用构造函数 2 的参数保存数据?
最佳答案
这是一个有趣的问题。我以前没有遇到过这种情况。然而,看完documentation ,它说,
First, create a database reference to your user data. Then use set() / setValue() to save a user object to the database with the user's username, full name, and birthday. You can pass set a string, number, boolean, null, array or any JSON object. Passing null will remove the data at the specified location.
我建议您像这样构建第二个构造函数。
public User(String name, String nationality) {
this.name = name;
this.nationality = nationality;
this.age = null;
this.eyeColor = null;
}
我希望这不会将添加的信息保存在用户 key 中。让我知道是否有帮助!
关于java - 使用一个类的多个constrictor在firebase中存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50914215/
我在使用 super(i, Material.wood) 行时遇到问题。它说“constrictor call must be the fist statement in a constructor”
我是一名优秀的程序员,十分优秀!