如何将数据对象从房间关系转换为Android Kotlin中的自定义父子-Child类型对象

发布于 2025-01-25 03:54:36 字数 700 浏览 2 评论 0原文

假设我有两个桌子父母和孩子。

这是父表模式 这是子表模式

当我们在这两个表上申请加入(类似)时, 这两个表的关系。但这返回包含父和子数据的对象列表。但是模型对嵌套回收器视图的要求是这样的亲子对象。 parent Model 儿童模型

因此,主要问题是在将关系应用于父母的关系后转换列表 -儿童类型列表。

我是一个新鲜的,并且对房间DB的了解不多,如果我遵循错误的方式,请让我纠正它。

谢谢。

Suppose I have two tables Parent and child.

This is the Parent Table Schema
This is the Child Table schema

when we apply to join(Relation) on these two table like this
Relation on these two tables. But this returns a list of objects that contain both parent and child data. But the requirement of the model for nested recycler view is the parent-child object like this.
Parent Model
Child Model

So, the main problem is to convert the list getting after applying relation to the parent-child type list.

I am a fresher and not so much knowledge about Room DB, if I follow some wrong way please let me correct it.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2025-02-01 03:54:36

替换父母和孩子的关系:

data class BagWithSkuViewModel (
    @Embedded
    val bag: BagEntity,
    @Relation(parentColumn = "bag_uid", entityColumn = "bag_id", entity = SkuEntity.class)
    val skus: List<SkuEntity>
)

现在您可以将其转换为父模型,更改为数据类:

data class HeadBagModel(var bagUid: String? = null, 
    var bagWeight: String? = null, 
    var bagStatus: String? = null, 
    var skuList: ArrayList<ScuEntity>? = null)

fun BagWithSkuViewModel.toHeadBagModel(): HeadBagModel {
    return HeadBagModel(this.bag.bag_uid, 
        this.bag.bag_weight, 
        this.bag.bagStatus, 
        this.skus) // your SkuEntity and ChildSkuModel are almost same, but if you need ChildSkuModel here, you can add another converter
}

Replace parent and child in relation:

data class BagWithSkuViewModel (
    @Embedded
    val bag: BagEntity,
    @Relation(parentColumn = "bag_uid", entityColumn = "bag_id", entity = SkuEntity.class)
    val skus: List<SkuEntity>
)

Now you can convert it to your Parent Model, changed to data class:

data class HeadBagModel(var bagUid: String? = null, 
    var bagWeight: String? = null, 
    var bagStatus: String? = null, 
    var skuList: ArrayList<ScuEntity>? = null)

fun BagWithSkuViewModel.toHeadBagModel(): HeadBagModel {
    return HeadBagModel(this.bag.bag_uid, 
        this.bag.bag_weight, 
        this.bag.bagStatus, 
        this.skus) // your SkuEntity and ChildSkuModel are almost same, but if you need ChildSkuModel here, you can add another converter
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文