Hello Guys, I'm using Android Architecture Compone...
# android
c
Hello Guys, I'm using Android Architecture Components Room in my project and I have an
Entity
with a
Enum
class as a property. It's something like this:
Copy code
@Entity(tableName = "tb_episode")
data class Episode (

    var title: String,
    
    var episodeType: EpisodeType
    ...
)
And I have the
TypeConverter
for it:
Copy code
object Converters {

    @TypeConverter
    @JvmStatic
    fun toEnum(code: Int): EpisodeType {
        return when (code) {
            1 -> EpisodeType.PODCAST
            2 -> EpisodeType.ALBUM_REVIEW
            else -> EpisodeType.DEFAULT
        }
    }

    @TypeConverter
    @JvmStatic
    fun fromEnum(type: EpisodeType): Int {
        return when (type) {
            EpisodeType.PODCAST -> 1
            EpisodeType.ALBUM_REVIEW -> 2
            else -> 0
        }
    }
}
Also, I added the
TypeConverters
Annotation to my Database class:
Copy code
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase()
But when I try to run the App I get this message:
Class is referenced as a converter but it does not have any converter methods.
I don't know if Room doesn't support Kotlin
Enum
class or if I'm doing something wrong. Anyone can help me with this?
m
@Carlos Ottoboni Instead of having your converter class be an
object
, make it an ordinary class and see if you get a different error message.
c
Hi there, I've made the changes but I'm still getting the same error.
Copy code
class EpisodeTypeConverter {

    @TypeConverter
    fun toEpisodeType(value: Int): EpisodeType {
        return EpisodeType.values()[value]
    }

    @TypeConverter
    fun fromEpisodeType(value: EpisodeType): Int {
        return value.let { value.ordinal }
    }
}
Copy code
@Entity(tableName = "tb_episode")
@TypeConverters(EpisodeTypeConverter::class)
data class Episode(
         ...
        @ColumnInfo(name = "episode_type")
        var episodeType: EpisodeType = EpisodeType.DEFAULT
)