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

时间:2019-04-02 00:06:58

标签: java android firebase firebase-realtime-database enums

首先,我认为我只是在项目后期才开始使用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 个答案:

答案 0 :(得分:0)

Firebase数据库不支持枚举数据类型。要查看有关支持的数据类型的更多信息,请检查Supported data types

解决方案1 ​​:(最简单) 只需在几天中使用字符串,而无需进行枚举

将dayOfTheWeek更改为字符串

String dayOfTheWeek; // Changed from Day to String

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

    this.dayOfTheWeek = dayOfTheWeek;
    this.openingTime = openingTime;
    this.closingTime = closingTime;

}

并在singleOpeningHours列表中使用如下字符串,

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:将SingleOpeningHours对象中使用的dayOfTheWeek更改为字符串,并使用Enum Class中的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;

}

并在singleOpeningHours列表中使用枚举日(此处无变化)

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"));

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