小编Kyl*_*ken的帖子

使用 Kotlin 在 Android Room 数据库中插入对象列表

Android 初学者正在努力克服 Room Database 的这一限制。我正在使用两张桌子,“服装”和“服装”。用户可以通过插入提供给他们的值来创建服装。然后,在单独的页面上,用户可以插入带有他们已在 Clothing.kt 中创建的先前服装的服装。出于应用程序的考虑,关系只是一对多,这意味着我只需要使用许多服装项目创建一套服装。到目前为止,这是我的代码:

服装.kt

@Parcelize
@Entity(foreignKeys = [
    ForeignKey(entity = Outfit::class,
        parentColumns = ["id"],
        childColumns = ["outfitRefFK"]
        )
    ]
)
data class Clothing (
    //Sets all attributes and primary key
    @PrimaryKey(autoGenerate = true) val id: Int,
    val type: String,
    val color: String,
    val style: String,
    val description: String,
    val dateAdded: Date = Date(),
    val brand: String,
    val theme: String,
    val image: String,
    @Nullable val outfitRefFK: Int
    ): Parcelable
Run Code Online (Sandbox Code Playgroud)

服装.kt

@Parcelize
@Entity
data class Outfit (
    @PrimaryKey(autoGenerate …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-room

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-room ×1

kotlin ×1