Android room persistent library - TypeConverter error of error: Cannot figure out how to save fie...

Room存储集合的时候,需要类型转换

e: /Users/jake/Documents/code/riseon_android/riseon-parents-android/app/build/tmp/kapt3/stubs/onlineDebug/com/risecenter/parent/data/local/entity/TaskRecentEntity.java:28: 错误: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
    private com.risecenter.model.UploadImage tasks;

解决步骤

1. 加注解 @TypeConverters ,注意不是 @TypeConverter,这样会报错
/**
 * 
 *     author : jake
 *     time   : 2018/07/31
 *     function   : 近期任务表(家庭作业)
 *     version: 1.0
 * 
*/ @Entity(tableName = "rise_task_recent", primaryKeys = ["id"]) data class TaskRecentEntity( val id: String, // 过期时间 var finished_at: String?, // 完成/未完成 var status: String?, // 通过/驳回 var submit_status: String?, // 过期/未过期 var date_status: String?, // 任务详情 @TypeConverters(TasksConverter::class) var tasks: List? )

就因为上面注解的错了,折腾了好久

Android room persistent library - TypeConverter error of error: Cannot figure out how to save fie..._第1张图片
编译不过去,重要的事情说三遍

重要的事情说三遍:
加注解 @TypeConverters ,注意不是 @TypeConverter
加注解 @TypeConverters ,注意不是 @TypeConverter
加注解 @TypeConverters ,注意不是 @TypeConverter

2. 转换类代码
/**
 * 
 *     author : jake
 *     time   : 2018/08/02
 *     function   : 任务详情数据转换类
 *     version: 1.0
 * 
*/ class TasksConverter { @TypeConverter fun getTasksFromString(value: String): List? { return Gson().fromJson>(value, object : TypeToken>() { }.type) } @TypeConverter fun storeTasksToString(tasks: List): String { return Gson().toJson(tasks) } }
3. bean
data class Tasks(
        val id: String?,
        val type: String?,
        val description: String?,
//        学生作业完成状态
        val finished: String?,
//        学生作业是否被驳回
        val rejected: String?,
        val resource: Resource?
)

你可能感兴趣的:(Android room persistent library - TypeConverter error of error: Cannot figure out how to save fie...)