Android Room自定义对象的类型转换器

huangapple go评论91阅读模式
英文:

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 = &quot;calorie_daily_table&quot;)
data class CalorieDaily(
@PrimaryKey
var date: String,
var breakfast: List&lt;Meal&gt;)

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&lt;FoodItem&gt;): String {
    return gson.toJson(foodItems)
}

@TypeConverter
fun stringToFoodItem(data: String): List&lt;FoodItem&gt; {
    val listType = object : TypeToken&lt;List&lt;FoodItem&gt;&gt;() {
    }.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&lt;Meal&gt;

答案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 = &quot;calorie_daily_table&quot;)
data class CalorieDaily(
@PrimaryKey
var date: String,
@TypeConverters(Converters::class)
var breakfast: List&lt;Meal&gt;)

data class Meal(
var foodItem: String,
var foodQuantity: String,
var calorie: Int)


class Converter {
var gson = Gson()

@TypeConverter
fun fromBreakfast(mealItems: List&lt;Meal&gt;): String {
return gson.toJson(mealItems)
}

@TypeConverter
fun toBreakfast(data: String): List&lt;Meal&gt; {
val listType = object : TypeToken&lt;List&lt;Meal&gt;&gt;() {
}.type
return gson.fromJson(data, listType)
}
}

huangapple
  • 本文由 发表于 2020年7月29日 21:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63154562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定