Search code examples
androidkotlinandroid-roomprimary-keyauto-generate

how to make an aotuGenerated field in a mutableList of parcelabel object in a feild of entity class Room kotlin


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 : enter image description here id for PipeLine working and increasing

enter image description here no for the DamagePoint not working and didn't increase

another output from emulator showing the point list int recyclerView enter image description here

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


Solution

  • 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