猿问

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

首先,我认为这是我的错,我只是在项目后期才开始使用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个枚举类,它们与正在存储的对象相关联。解决此问题的最佳方法是什么?提前感谢各位


蓝山帝景
浏览 122回答 1
1回答

jeck猫

火库数据库不支持枚举数据类型。若要查看有关支持的数据类型的详细信息,请检查支持的数据类型解决方案1:(最简单的)只需使用字符串即可不进行枚举将星期的日期更改为字符串String dayOfTheWeek; // Changed from Day to Stringpublic SingleOpeningHours(String dayOfTheWeek,String openingTime, String closingTime){&nbsp; &nbsp; this.dayOfTheWeek = dayOfTheWeek;&nbsp; &nbsp; this.openingTime = openingTime;&nbsp; &nbsp; this.closingTime = closingTime;}并在单个打开时间列表中使用字符串,如下所示,ArrayList<SingleOpeningHours> singleOpeningHours = new ArrayList<SingleOpeningHours>();&nbsp; &nbsp; singleOpeningHours.add(new SingleOpeningHours("MONDAY", "09:00", "00:00"));&nbsp; &nbsp; singleOpeningHours.add(new SingleOpeningHours("TUESDAY", "09:00", "01:00"));解决方案 2:将“单打开时间”对象中使用的“每周”日期更改为字符串,并在枚举类中使用 name() 方法将枚举值转换为字符串String dayOfTheWeek; // Changed from Day to StringString 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"));希望这些解决方案将帮助您解决问题
随时随地看视频慕课网APP

相关分类

Java
我要回答