为了账号安全,请及时绑定邮箱和手机立即绑定

在火库中存储/检索枚举的解决方法是什么?

在火库中存储/检索枚举的解决方法是什么?

蓝山帝景 2022-09-14 17:53:32
首先,我认为这是我的错,我只是在项目后期才开始使用Firebase。无论如何,我想做的是将Pub对象存储到数据库中。它包含各种信息,例如开放时间,评级,设施等。问题不在存储中,而是每当我试图从数据库中获取酒吧列表时,我都会遇到这个问题这是错误。 com.google.firebase.database.DatabaseException: Could not find enum value of com.example.matasolutions.pintindex.Day for value "Monday"        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToEnum(com.google.firebase:firebase-database@@16.1.0:302)        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:212)        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@16.1.0:178)这是枚举类。public enum Day {    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY};这就是具有枚举的构造函数的外观public SingleOpeningHours(Day dayOfTheWeek,String openingTime, String closingTime){        this.dayOfTheWeek = dayOfTheWeek;        this.openingTime = openingTime;        this.closingTime = closingTime;    }最后,这就是它如何被添加到列表中,然后添加到数据库中。  ArrayList<SingleOpeningHours> singleOpeningHours = new ArrayList<SingleOpeningHours>();        singleOpeningHours.add(new SingleOpeningHours(Day.MONDAY, "09:00", "00:00"));        singleOpeningHours.add(new SingleOpeningHours(Day.TUESDAY, "09:00", "01:00"));        singleOpeningHours.add(new SingleOpeningHours(Day.WEDNESDAY, "09:00", "01:00"));        singleOpeningHours.add(new SingleOpeningHours(Day.THURSDAY, "09:00","01:00"));        singleOpeningHours.add(new SingleOpeningHours(Day.FRIDAY, "09:00", "01:00"));        singleOpeningHours.add(new SingleOpeningHours(Day.SATURDAY, "09:00", "01:00"));        singleOpeningHours.add(new SingleOpeningHours(Day.SUNDAY, "09:00", "01:00"));编辑:我有7个枚举类,它们与正在存储的对象相关联。解决此问题的最佳方法是什么?提前感谢各位
查看完整描述

1 回答

?
jeck猫

TA贡献1909条经验 获得超7个赞

火库数据库不支持枚举数据类型。若要查看有关支持的数据类型的详细信息,请检查支持的数据类型


解决方案1:(最简单的)只需使用字符串即可不进行枚举


将星期的日期更改为字符串


String dayOfTheWeek; // Changed from Day to String


public SingleOpeningHours(String dayOfTheWeek,String openingTime, String closingTime){


    this.dayOfTheWeek = dayOfTheWeek;

    this.openingTime = openingTime;

    this.closingTime = closingTime;


}

并在单个打开时间列表中使用字符串,如下所示,


ArrayList<SingleOpeningHours> singleOpeningHours = new ArrayList<SingleOpeningHours>();


    singleOpeningHours.add(new SingleOpeningHours("MONDAY", "09:00", "00:00"));

    singleOpeningHours.add(new SingleOpeningHours("TUESDAY", "09:00", "01:00"));

解决方案 2:将“单打开时间”对象中使用的“每周”日期更改为字符串,并在枚举类中使用 name() 方法将枚举值转换为字符串


String dayOfTheWeek; // Changed from Day to String

String openingTime;

String closingTime;


public SingleOpeningHours(Day dayOfTheWeek,String openingTime, String closingTime){


this.dayOfTheWeek = dayOfTheWeek.name(); // make sure add .name()

this.openingTime = openingTime;

this.closingTime = closingTime;

}


并在单个打开时间列表中使用枚举日(此处无更改)


ArrayList<SingleOpeningHours> singleOpeningHours = new ArrayList<SingleOpeningHours>();


singleOpeningHours.add(new SingleOpeningHours(Day.MONDAY, "09:00", "00:00"));

singleOpeningHours.add(new SingleOpeningHours(Day.TUESDAY, "09:00", "01:00"));

希望这些解决方案将帮助您解决问题


查看完整回答
反对 回复 2022-09-14
  • 1 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信