有没有办法将 Key 存储在我从 Firebase 对象投射的类中? [英] Is there a way to store Key in class which I cast from Firebase object?
问题描述
这是我的数据库:
[x] database
-- > [+] students
-----> [+] -KuGacomJymBwkcc7zlU (pushKey)
-----------> Name - Jon
-----------> Age - 21
我有 Student.class :
I have Student.class :
String name;
String age;
public Student(String Name, String Age) {
this.name=Name;
this.age=Age;
}
我从 firebase 数据快照中读取信息,如下所示:
I read the information from firebase datasnapshot like this:
Student newStudent = DataSnapshot.getValue(Student.class);
当我这样做时,我得到了姓名和年龄,但我的问题是是否有办法将 ID (pushKey) 存储在 Student 类上而不添加一个字符串来保存它并在 firebase 数据库上获取另一个字段.
When I do this I get the name and the age, but my question if there is a way to store the ID (pushKey) on the Student class without adding a String that will hold it and take another field on the firebase database.
谢谢大家.
推荐答案
我更喜欢将键和值分开,或者以其他方式传递 DataSnapshot
.但是,如果您愿意,您也可以提取密钥 (DataSnapshot.getKey()
) 并将其设置在您 @Exclude
的 Java 类中的属性上:
I prefer keeping the keys and the values separate, or otherwise passing the DataSnapshot
s around. But if you want, you can also extract the key (DataSnapshot.getKey()
) and set it on a property in your Java class that you @Exclude
:
public class Student {
@Exclude
public String key;
public String name;
public String age;
}
然后:
Student newStudent = snapshot.getValue(Student.class);
newStudent.key = snapshot.getKey();
相关:
这篇关于有没有办法将 Key 存储在我从 Firebase 对象投射的类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!