如何在实体类 Room kotlin 字段中的 Parcelabel 对象的 mutableList 中创建 aotuGenelated 字段
我有一个实体类(PipeLine),它有一个parcelabel类(DamagePoint)的可变列表 主类 PipeLine 有一个字段 val id:Int=0,
主键设置为 autoGerat=true 它工作正常
子类 DamagePoint 也有一个主键 val no:Int=1,< /code> 我用它来表示点序列,但它不起作用! 生成的所有点都有 no=0
只是为了澄清,每个 PipeLine 都有一个 DamagePoints 列表,按数字排序
我该怎么做!
PipeLine.kt 类
@Parcelize
@Entity(tableName = "table_Lines")
data class PipeLine(
@PrimaryKey(autoGenerate = true)
val id:Int=0,
var name: String?,
var ogm: String?,
var length: String?,
var type: String?,
var i_start: String?,
var i_end: String?,
var start_point: String?,
var end_point: String?,
var work_date:String?,
var points: MutableList<DamagePoint>
):Parcelable
@Parcelize
data class DamagePoint(
@PrimaryKey(autoGenerate = true)
val no:Int=1,
var db: String? = null,
var depth: String? = null,
var current1: String? = null,
var current2: String? = null,
var gps_x: String? = null,
var gps_y: String? = null
):Parcelable
class DataConverter {
@TypeConverter
fun fromPoints(points: MutableList<DamagePoint>?): String? {
if (points == null) {
return null
}
val gson = Gson()
val type = object : TypeToken<MutableList<DamagePoint>?>() {}.type
return gson.toJson(points, type)
}
@TypeConverter
fun toPoints(points: String?): MutableList<DamagePoint>? {
if (points == null) {
return null
}
val gson = Gson()
val type = object : TypeToken<MutableList<DamagePoint>?>() {}.type
return gson.fromJson<MutableList<DamagePoint>>(points, type)
}
}
输出: PipeLine 工作和增加的 id
no 表示 DamagePoint 不起作用,并且没有增加
模拟器的另一个输出,显示点列表 int recyclerView
是主键,在类型转换器中不起作用 或者有另一种方法可以做到这一点并使其增加? 请有任何帮助
我用 @PrimaryKey 注释它并将 autoGenerate 设置为 true 尝试在 val 和 var 之间切换,同样的事情
I have an entity class (PipeLine) that has a Mutable list of a parcelabel class (DamagePoint )
the main class PipeLine has a field val id:Int=0,
primary key set to autoGerat=true it's working fine
the subclass DamagePoint also has a primary key val no:Int=1,
I use it for points sequence it's not working!!
all the points generated has the no=0
Just to clarify, each PipeLine has a list of DamagePoints, sequenced by numbers
how would I do that !!
the class PipeLine.kt
@Parcelize
@Entity(tableName = "table_Lines")
data class PipeLine(
@PrimaryKey(autoGenerate = true)
val id:Int=0,
var name: String?,
var ogm: String?,
var length: String?,
var type: String?,
var i_start: String?,
var i_end: String?,
var start_point: String?,
var end_point: String?,
var work_date:String?,
var points: MutableList<DamagePoint>
):Parcelable
@Parcelize
data class DamagePoint(
@PrimaryKey(autoGenerate = true)
val no:Int=1,
var db: String? = null,
var depth: String? = null,
var current1: String? = null,
var current2: String? = null,
var gps_x: String? = null,
var gps_y: String? = null
):Parcelable
class DataConverter {
@TypeConverter
fun fromPoints(points: MutableList<DamagePoint>?): String? {
if (points == null) {
return null
}
val gson = Gson()
val type = object : TypeToken<MutableList<DamagePoint>?>() {}.type
return gson.toJson(points, type)
}
@TypeConverter
fun toPoints(points: String?): MutableList<DamagePoint>? {
if (points == null) {
return null
}
val gson = Gson()
val type = object : TypeToken<MutableList<DamagePoint>?>() {}.type
return gson.fromJson<MutableList<DamagePoint>>(points, type)
}
}
output :
id for PipeLine working and increasing
no for the DamagePoint not working and didn't increase
another output from emulator showing the point list int recyclerView
is the primaryKey don't work in the type converter
or there is another way to do it and make it increase?
please anything would help
I annotate it with @PrimaryKey and set autoGenerate to true
tried to switch between val and var , same thing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于您为
no
设置的1
整数。这样,它就不能作为自动增量工作,因为有一个指定的整数值(1
)而不是0
。因此,这意味着如果您将
no
设置为0
,它将像未设置一样工作,并会增加值。此外,您可能需要将
no
名称更改为id
。The problem is with the
1
integer which you set for theno
. In this way, it does not work as auto-increment since there is a specified integer value (1
) instead of0
.So it means that if you set
no
to0
, it will work as unset and will increase the values.Also, you may need to change the
no
name toid
.问题是我每次想在管道实体中的点列表中添加一个点列表时,我试图添加一个损坏点的完整列表对象,因此找到所有点(no)等于1是正常的,因为它是它的默认值
现在,我在更新并将其添加到管道对象之前,将其添加到列表中的每个点的否
我找到了一种仅更新管道对象中的点列表的方法,而无需更新整个对象
@query(“ update table_lines设置点=:点id =:id”)
娱乐updatepointslist(id:int,点:mutablelist),
如果有人知道如何将项目添加到可变列表中,该列表是实体类的属性
无需更新列表或更新实体对象
请添加答案
the problem is I was trying to add a full list of DamagePoint every time I wanted to add a point to the points list in the PipeLine entity so I fetch the list from the object add new points to it and update the entire list for that pipeline object so it's normal to find all the point (no) equal to 1 because it's the default value for it
now I handle the no of each point I add to the list before updating it and adding it back to the pipeline object
I found a way to update only the points list in the pipeline object without updating the entire object
@Query("UPDATE table_Lines SET points=:points WHERE id=:id ")
fun updatePointsList(id:Int,points:MutableList)
if someone knows how to add an item to a mutable list which is a property of the entity class
without updating the list or updating the entity object
please add an answer