英文:
Android Room Type Converter for Custom Object
问题
错误信息指出无法将自定义对象列表保存到数据库中,建议考虑添加类型转换器。您的数据类定义如下:
@Entity(tableName = "calorie_daily_table")
data class CalorieDaily(
@PrimaryKey
var date: String,
var breakfast: List<Meal>
)
data class Meal(
var foodItem: String,
var foodQuantity: String,
var calorie: Int
)
要处理这种情况,您可以编写一个类型转换器(TypeConverter)。以下是一个示例类型转换器的代码:
class Converter {
var gson = Gson()
@TypeConverter
fun mealListToString(meals: List<Meal>): String {
return gson.toJson(meals)
}
@TypeConverter
fun stringToMealList(data: String): List<Meal> {
val listType = object : TypeToken<List<Meal>>() {}.type
return gson.fromJson(data, listType)
}
}
请确保在数据库配置中使用此类型转换器,以便Room知道如何将List<Meal>
转换为字符串并存储到数据库中。这应该解决您遇到的错误。
英文:
I'm getting an error while using a list of custom object with Room.
error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
So my data class is like this:
@Entity(tableName = "calorie_daily_table")
data class CalorieDaily(
@PrimaryKey
var date: String,
var breakfast: List<Meal>)
data class Meal(
var foodItem: String,
var foodQuantity: String,
var calorie: Int)
How can i write a TypeConverter to handle this?
So, I've something like this:
class Converter {
var gson = Gson()
@TypeConverter
fun foodItemToString(foodItems: List<FoodItem>): String {
return gson.toJson(foodItems)
}
@TypeConverter
fun stringToFoodItem(data: String): List<FoodItem> {
val listType = object : TypeToken<List<FoodItem>>() {
}.type
return gson.fromJson(data, listType)
}
}
But, still getting the same error.
答案1
得分: 1
你需要将注解@Embedded
添加到你的第二个数据类Meal
中。
英文:
You have to add the annotation @Embedded
to your second data class Meal
答案2
得分: 0
尝试在早餐之前添加@TypeConverters。
@TypeConverters(Converter::class)
var breakfast: List<Meal>
英文:
Try adding @TypeConverters before breakfast.
@TypeConverters(Converter::class)
var breakfast: List<Meal>
答案3
得分: 0
更新的工作代码:
@Entity(tableName = "calorie_daily_table")
data class CalorieDaily(
@PrimaryKey
var date: String,
@TypeConverters(Converters::class)
var breakfast: List<Meal>)
data class Meal(
var foodItem: String,
var foodQuantity: String,
var calorie: Int)
class Converter {
var gson = Gson()
@TypeConverter
fun fromBreakfast(mealItems: List<Meal>): String {
return gson.toJson(mealItems)
}
@TypeConverter
fun toBreakfast(data: String): List<Meal> {
val listType = object : TypeToken<List<Meal>>() {
}.type
return gson.fromJson(data, listType)
}
}
英文:
Updated working code:
@Entity(tableName = "calorie_daily_table")
data class CalorieDaily(
@PrimaryKey
var date: String,
@TypeConverters(Converters::class)
var breakfast: List<Meal>)
data class Meal(
var foodItem: String,
var foodQuantity: String,
var calorie: Int)
class Converter {
var gson = Gson()
@TypeConverter
fun fromBreakfast(mealItems: List<Meal>): String {
return gson.toJson(mealItems)
}
@TypeConverter
fun toBreakfast(data: String): List<Meal> {
val listType = object : TypeToken<List<Meal>>() {
}.type
return gson.fromJson(data, listType)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论