How to save enum field in the database room?

后端 未结 4 541
不思量自难忘°
不思量自难忘° 2020-12-10 11:47

I must write the value from the enum enumeration to the database. An error occurs during compilation. What am I doing wrong?

Cannot figur

4条回答
  •  粉色の甜心
    2020-12-10 11:57

    Enum class;

    enum class Priority {
    HIGH,
    MEDIUM,
    LOW
    }
    

    Converter class;

    class Converter {
    
    @TypeConverter
    fun fromPriority(priority: Priority): String {
        return priority.name
    }
    
    @TypeConverter
    fun toPriority(priority: String): Priority {
        return Priority.valueOf(priority)
    }
    
    }
    

    usage;

    @Database(entities = [MyData::class], version = 1, exportSchema = false)
    @TypeConverters(Converter::class)
    abstract class MyDatabase : RoomDatabase() {
    
      // todo
    
    }
    

提交回复
热议问题