有没有一种方法可以将Key存储在Firebase对象所投的类中? [英] Is there a way to store Key in class which I cast from Firebase object?

查看:151
本文介绍了有没有一种方法可以将Key存储在Firebase对象所投的类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  [x] database 
- > [+]学生
-----> [+] -KuGacomJymBwkcc7zlU(pushKey)
----------->名称 - Jon
----------->年龄 - 21

我有Student.class:

 字符串名称; 
字符串年龄;

public Student(String Name,String Age){
this.name = Name;
this.age =年龄;

$ / code>

我从firebase datasnapshot读取这些信息:

 学生newStudent = DataSnapshot.getValue(Student.class); 

当我这样做的时候,我得到了名字和年龄,但是我的问题是否有办法将ID(pushKey)存储在Student类中,而不需要添加一个字符串来保存它,并在Firebase数据库中取另一个字段。

谢谢大家。

解决方案

我更喜欢保持键和值分开,否则传递 DataSnapshot 周围。但是,如果你愿意,你也可以提取键( DataSnapshot.getKey()),并将其设置在Java类中的一个属性排除

  public class Student {
@Exclude
public String key ;
公共字符串名称;
public String age;

$ / code>

然后:

  Student newStudent = snapshot.getValue(Student.class); 
newStudent.key = snapshot.getKey();

相关:


This is my database :

[x] database
-- > [+] students
-----> [+] -KuGacomJymBwkcc7zlU (pushKey)
-----------> Name - Jon
-----------> Age - 21

I have Student.class :

String name;
String age;

public Student(String Name, String Age) {
this.name=Name;
this.age=Age;
}

I read the information from firebase datasnapshot like this:

Student newStudent = DataSnapshot.getValue(Student.class);

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.

Thank you all.

解决方案

I prefer keeping the keys and the values separate, or otherwise passing the DataSnapshots 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;
}

And then:

Student newStudent = snapshot.getValue(Student.class);
newStudent.key = snapshot.getKey();

Related:

这篇关于有没有一种方法可以将Key存储在Firebase对象所投的类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆