使用类型转换器将列表转换为字符串,反之亦然(Kotlin)

qf9go6mv  于 2023-03-09  发布在  Kotlin
关注(0)|答案(1)|浏览(183)

我有问题转换Lists-〉Strings和'字符串'到'列表'的类型转换器在android工作室。

实体

data class Results(
@PrimaryKey(autoGenerate = true)
val id:Int? = null,

val category: List<String>,

val content: String?,

val country: List<String>?,

val creator: List<String>?,

val description: String?,

val image_url: String?,

val keywords: List<String>?,

val language: String?,

val link: String?,

val pubDate: String?,

val source_id: String?,

val title: String?,

val video_url: String?
)

类型转换器

class Converters() {

    @TypeConverter
    fun listToJsonString(value: List<String>?): String? = Gson().toJson(value)

    @TypeConverter
    fun jsonStringToList(value: String?) =
            Gson().fromJson(value, Array<String>::class.java).toList()
}

这是应该工作,但我得到了这个错误Gson().fromJson(value, Array<String>::class.java) must not be null,我假设我需要确保jsonStringToList中的value参数不是null
我该怎么做呢?

shyt4zoc

shyt4zoc1#

使用Room2.5.0和com.google.code.gson库2.10.1以及core.ktx1.9.0可以编译并运行代码。
例如:

....
val r1 = Results(
            category = listOf(),
            content = "blah",
            country = listOf(),
            creator = listOf(),
            description = "blah",
            image_url = "nowhere",
            keywords = listOf(),
            language = "gibberish",
            link = "the link",
            pubDate = "2023-01-01",
            source_id = "somewhere",
            title = "This",
            video_url = ""
        )
        daoDB1.insert(r1)
        for (r in daoDB1.getAllResults()) {
            Log.d("DBINFO","Result is:- " +
                    "Content={${r.content}} " +
                    "Country=${r.country} " +
                    "Desc=${r.description} " +
                    "Img URL=${r.image_url} " +
                    "Lang=${r.language} " +
                    "Link= ${r.link} " +
                    "PubDate=${r.pubDate} " +
                    "Source=${r.source_id} " +
                    "Title=${r.title} " +
                    "VidURL=${r.video_url} " +
                    "#of Categories is ${r.category.size}" +
                    "#of Creators is ${r.creator!!.size} " +
                    "#of Keywords is ${r.keywords!!.size} " +
                    "")
        }

结果:-
日志包含:-

D/DBINFO: Result is:- Content={blah} Country=[] Desc=blah Img URL=nowhere Lang=gibberish Link= the link PubDate=2023-01-01 Source=somewhere Title=This VidURL= #of Categories is 0#of Creators is 0 #of Keywords is 0

和应用程序检查显示:-

    • 但是**

如果您指定一个空值而不是一个空列表,那么在尝试提取数据时,您将得到一个空指针异常。
例如,如果country = listOf(),更改为country = null,,则:-

Caused by: java.lang.NullPointerException: Gson().fromJson(value, Array<String>::class.java) must not be null
        at a.a.so75623951kotlinroommultipledifferentdatabases.Converters.jsonStringToList(DBStuff.kt:62)
        at a.a.so75623951kotlinroommultipledifferentdatabases.AllDao_Impl.getAllResults(AllDao_Impl.java:247)
        at a.a.so75623951kotlinroommultipledifferentdatabases.MainActivity.onCreate(MainActivity.kt:41)

即使应用程序检查显示该行已成功插入:-

  • 注:第3行是由于再次运行而未尝试提取,因为如果存在异常,则数据库将不可用,因为进程已分离。即,添加行只是为了允许应用程序检查查看数据库。

简而言之,您应该保存一个空列表,并且不允许空值,否则使用TypeConverter会在遇到空值时导致异常。因此,您应该始终使用List<String>,而不是List<String>?

  • 至少可以使用com.google.code.gson库。

相关问题