问题描述
在我的应用程序中,我有一个模型类,它有一些变量,我可以使用改造和Room DB在此应用程序中调用并显示此数据.这意味着该应用程序首先从服务器收集数据,然后显示在会议室DB中.但是,当我在此模型类中使用列表时,它会显示此错误.这是我的代码
In my app I have a model class, it has some variables, I can call and show this data in this app using retrofit and room DB. which means this app first collects data from the server then it shows in room DB. But when I am using the list in this model class it shows this error. Here is my code
Movie.kt
@Entity(tableName = "movie_table")
data class Movie(
@SerializedName("Id")
@PrimaryKey(autoGenerate = true)
val id: Int,
@SerializedName("Title")
@Expose
val title: String,
@SerializedName("Year")
@Expose
val Year: Int,
@SerializedName("Actors")
@Expose
val actorDetails: List<Actor>
)
Actor.kt
data class Actor(
@SerializedName("ActorName")
@Expose
val actorName: String,
@SerializedName("ActorPoster")
@Expose
val actorImage: String
)
MovieDao.kt
MovieDao.kt
@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMovie(movie: Movie)
@Query("SELECT * FROM movie_table")
suspend fun getAllMovieDB(): List<Movie>
}
MovieDatabase.kt
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
这是我的假JSON API在此处输入链接描述
here is my fake JSON APIenter link description here
这是错误在此处输入图片描述我找不到任何错误,我也正在使用分析来获取错误,但是什么也没显示.我该如何解决?谢谢.
here is the errorenter image description hereI can't find any error, I am also using analyze for get the error but it's show nothing.How can I solve this? Thank you.
推荐答案
首先,您必须从room使用@TypeConverter,因为room无法插入列表,对象或位图之类的自定义类型.因此,首先创建一个名为converter的类,然后使用注释@TypeConverters在数据库中添加该类.这是代码
at first you have to use @TypeConverter from room because room can not insert the custom types like List or object or bitmap. So firstly make a class named converter then add this class in database using annotation @TypeConverters.here is the code
Converter.kt
Converter.kt
class Converter {
@TypeConverter
fun fromActor(actor: List<Actor>):String{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().toJson(actor,type)
}
@TypeConverter
fun toActor(actorString: String): List<Actor>{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().fromJson<List<Actor>>(actorString,type)
}
}
最后将这个converter.kt类添加到您的数据库中
and finally add this converter.kt class in your database
MovieDatabase.kt
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
@TypeConverters(Converter::class)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
谢谢.
这篇关于我遇到此错误执行org.jetbrains.kotlin.gradle.internal.KaptExecution时发生故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!