Firebase android基于类的更新不考虑字段名称的大小写 [英] firebase android class-based updates not respecting case of the field names
问题描述
我声明了以下类(请注意大小写选择):
I have the following class declared (note the case choice):
public class User{
private String DisplayName;
private Boolean Proxy = false;
@SuppressWarnings("unused")
public User(){
}
public User(String DisplayName, Boolean Proxy) {
this.DisplayName = DisplayName;
this.Proxy = Proxy;
}
public String getDisplayName() {
return DisplayName;
}
public void setDisplayName(String DisplayName) {
this.DisplayName = DisplayName;
}
public Boolean getProxy() {
return Proxy;
}
}
然后我打电话给
ref.child("People/123").setValue(userList);
其中 userList
是 ArrayList< User>
但是,创建的节点并不像我想象的那样尊重大写字母.他们创建 displayName
和 proxy
,而不是预期的 DisplayName
和 Proxy
以匹配声明.
However the nodes created do not respect the capitalization as I would imagine it to do. They create displayName
and proxy
instead of the expected DisplayName
and Proxy
to match the declaration.
这是预期的行为吗?有没有办法做到 尊重字段名?
Is this expected behavior? Is there a way to make it such that it does respect the field name?
推荐答案
您似乎假设Firebase存储中的名称基于您的字段 DisplayName
和 Proxy
.
You seem to assume that the names in the Firebase storage are based on your fields DisplayName
and Proxy
.
但是,如果您阅读 setValue
上的文档它说:
But if you read the documentation on setValue
it says:
该类必须为要分配的属性定义公共获取方法.反序列化实例时,没有公共获取器的属性将设置为其默认值
The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
这听起来像Firebase使用 Java bean属性定义(因为Java没有本地属性),这意味着它们是从(公共)方法名称 getDisplayName
和 getProxy
派生的.
This reads like Firebase uses the Java bean definition of properties (since Java doesn't have native properties), which means they are derived from the (public) method names getDisplayName
and getProxy
.
要从 getDisplayName
中确定属性名称,请去除 get
前缀,并在该小写字母后添加第一个字母.正是这样导致您在Firebase存储中看到的属性名称.
To determine the property name from getDisplayName
you strip the get
prefix and make the first letter after that lowercase. And that leads exactly to the property names you're seeing in your Firebase storage.
这篇关于Firebase android基于类的更新不考虑字段名称的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!