我的班级是CredicardinfoObject.class
public class CreditcardinfoOjbect implements Serializable { private static final long serialVersionUID = 1L; public static String Title; public static String startTime; public static String endTime; public static String day;}
这个类只有静态的特性.没有实例化需要.
在我的mainActivity.class中,我想设置类的值,然后存储此firebase数据库.这是我的mainActivity.class.
CreditcardinfoOjbect.Title = "foo"; CreditcardinfoOjbect.startTime = "bar"; CreditcardinfoOjbect.day = "1"; CreditcardinfoOjbect.endTime = "2"; final FirebaseDatabase database = FirebaseDatabase.getInstance(); String uID = ""; FirebaseAuth auth = FirebaseAuth.getInstance(); FirebaseUser user = auth.getCurrentUser(); if (user != null) { uID = user.getUID(); } DatabaseReference ref = database.getReference("users").child(uID).child("account").child("creditcards"); HashMap
不幸的是,这不起作用.如何在Firebase中存储类的当前值?最佳答案首先,您需要添加到您的pojo类,Firebase需要的无参数构造,以及每个字段的公共setter和公共getter,如下所示:
public class CreditCardInfoObject implements Serializable { private String Title; privat eString startTime; private String endTime; private String day; public CreditCardInfoObject() {} public String getTitle() {return Title;} public voID setTitle(String Title) {this.Title = Title;} public String getStartTime() {return startTime;} public voID setStartTime(String startTime) {this.startTime = startTime;} public String getEndTime() {return endTime;} public voID setEndTime(String endTime) {this.endTime = endTime;} public String getDay() {return day;} public voID setDay(String day) {this.day = day;}}
请注意,您不需要静态字段.另请注意,我将您的所有字段设置为私有.将这些字段设置为公共字段是一种非常糟糕的做法.
现在,要设置pojo类的值,需要创建CreditCardInfoObject类的新对象并使用公共setter.拥有此新对象后,您可以将其保存到Firebase数据库,如下所示:
CreditcardinfoOjbect obj = new CreditcardinfoOjbect();obj.setTitle("foo");obj.setStartTime("bar");obj.setEndTime("1");obj.setDay("2");DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();String uID = "";FirebaseAuth auth = FirebaseAuth.getInstance();FirebaseUser user = auth.getCurrentUser();if (user != null) { uID = user.getUID();}DatabaseReference ref = rootRef.child("users").child(uID).child("account").child("creditcards");ref.setValue(obj);
总结 以上是内存溢出为你收集整理的java – 将类保存到firebase全部内容,希望文章能够帮你解决java – 将类保存到firebase所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)