gpt4 book ai didi

database - 当数据类具有内部类时,房间数据库表创建错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:26:10 24 4
gpt4 key购买 nike

要求是解析 json 数据并使用 Kotlin 将其插入房间数据库。解析已完成,但在使用数据类创建表时出现问题,因为数据类具有内部类。无法使用内部类中的字段创建表。

无法理解如何使用@TypeConverters

@Entity(tableName = "tbl_newsData")
@TypeConverters(ReposPersistentConverter::class)
data class Article(
@PrimaryKey(autoGenerate = true) var _id: Long?,
@ColumnInfo(name = "author") var author: String?,
@ColumnInfo(name = "title") var title: String?,
@ColumnInfo(name = "description") var description: String?,
@ColumnInfo(name = "url") var url: String?,
@ColumnInfo(name = "urlToImage") var urlToImage: String?,
@ColumnInfo(name = "publishedAt") var publishedAt: String?,
@ColumnInfo(name = "content") var content: String?,
var source: Source?
){
constructor() : this(null,"", "", "", "", "",
"", "", "", "",null)
}

class ReposPersistentConverter {
val gson = Gson()
// RepoOwner
@TypeConverter
fun storeRepoOwnerToString(data: Source): String = gson.toJson(data)

@TypeConverter
fun storeStringToRepoOwner(value: String): Source = gson.fromJson(value)
}

@Entity(tableName = "tbl_newsData")
data class Article(
@PrimaryKey(autoGenerate = true) var _id: Long?,
@ColumnInfo(name = "author") var author: String?,
@ColumnInfo(name = "title") var title: String?,
@ColumnInfo(name = "description") var description: String?,
@ColumnInfo(name = "url") var url: String?,
@ColumnInfo(name = "urlToImage") var urlToImage: String?,
@ColumnInfo(name = "publishedAt") var publishedAt: String?,
@ColumnInfo(name = "content") var content: String?,
var source: Source? //inner class
){
constructor() : this(null,"", "", "", "", "",
"", "", "", "",null)
}

////源码类

data class Source(
val id: String,
val name: String
)

//json

{

"source": {
"id": null,
"name": "Geeksofdoom.com"
},
"author": "The Movie God",
"title": "Hulu Comings and Goings: What’s New and What’s Leaving In September 2019",
"description": "For those of you who do your entertainment streaming on Hulu, whether it be responsibly in moderation or recklessly in full-on binge watching sessions, there's plenty of TV shows and movies arriving and departing each month to keep track of. The titles set to…",
"url": "https://www.geeksofdoom.com/2019/08/26/hulu-comings-goings-new-leaving-september-2019",
"urlToImage": "https://www.geeksofdoom.com/GoD/img/2016/02/hulu.jpg",
"publishedAt": "2019-08-26T18:00:53Z",
"content": "For those of you who do your entertainment streaming on Hulu, whether it be responsibly in moderation or recklessly in full-on binge watching sessions, there’s plenty of TV shows and movies arriving and departing each month to keep track of.The titles set to … [+8407 chars]"

}

错误:无法确定如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。 私有(private) com.app.newsapp.dashboard.model.Source 来源;

最佳答案

@Entity(tableName = "tbl_newsData")
data class Article(
@PrimaryKey(autoGenerate = true) var _id: Long?,
@ColumnInfo(name = "author") var author: String?,
@ColumnInfo(name = "title") var title: String?,
@ColumnInfo(name = "description") var description: String?,
@ColumnInfo(name = "url") var url: String?,
@ColumnInfo(name = "urlToImage") var urlToImage: String?,
@ColumnInfo(name = "publishedAt") var publishedAt: String?,
@ColumnInfo(name = "content") var content: String?,
@TypeConverters(SourceTypeConverter::class)
@ColumnInfo(name = "source")
var source: Source?
){
class SourceTypeConverter {
@TypeConverter
fun fromDeliveryExchangeList(source: Source?): String? {
if (source == null) {
return null
}
val gson = Gson()
val type = object : TypeToken<Source>() {

}.type
return gson.toJson(source, type)
}

@TypeConverter
fun toDeliveryExchangeList(source: String?): Source? {
if (source == null) {
return null
}
val gson = Gson()
val type = object : TypeToken<Source>() {

}.type
return gson.fromJson(source, type)
}
}

constructor() : this(null,"", "", "",
"", "", "", "",null)
}

////////添加转换器

@Database(entities = arrayOf(Article::class), version = 1)
@TypeConverters(Article.SourceTypeConverter::class)
abstract class AppDataBase : RoomDatabase() {
}

关于database - 当数据类具有内部类时,房间数据库表创建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57663144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com