Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

android - How to save a list of objects in Room using Kotlin?

I need to save some data from my API in my Room database, so i've just created my model as the API response in my android project but when i try to run it the app require to set the Entity even to my object which is only set to give a definition of the object which will be saved in my database...

So which will be the right way to save an array of objects inside an object in my Room database?

Here is how my models looks like:

@Entity(tableName = "articoli_server_table")
data class ArticoliServer(
    @PrimaryKey(autoGenerate = false)
    var codart: String,
    var desc: String,
    var prezzo_acq: Float,
    var prezzo_vend: Float,
    var barcode: List<barcode>,
    var qta: Float
)


data class barcode(
    var barcode: String,
    var qta: Float
)
question from:https://stackoverflow.com/questions/65918227/how-to-save-a-list-of-objects-in-room-using-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are a couple of approaches you can implement to save your list of barcode data in your database:

1. you can use custom TypeConverter for your list of barcode objects, for example you can persist your list of objects as a single string in the following form (comma seperated values): "barcode1,qta1,barcode2,qta2,..", the implementation could be like this:

@Entity(tableName = "articoli_server_table")
@TypeConverters(BarcodeListConverter::class)
data class ArticoliServer(
    @PrimaryKey(autoGenerate = false)
    var codart: String,
    var desc: String,
    var prezzo_acq: Float,
    var prezzo_vend: Float,
    var barcode: List<barcode>?,
    var qta: Float
)


data class barcode(
    var barcode: String,
    var qta: Float
)


object BarcodeListConverter {
    @TypeConverter
    fun toString(barcodeList: List<barcode>?): String? {
        if (barcodeList == null) return null

        val stringList = mutableListOf<String>()
        barcodeList.forEach {
            stringList.add(it.barcode)
            stringList.add(it.qta.toString())
        }

        return stringList.joinToString(",")
    }

    @TypeConverter
    fun toBarcodeList(str: String?): List<barcode>? {
        if (str == null) return null

        val barcodeList = mutableListOf<barcode>()

        val strList = str.split(",")
        for (i in strList.indices step 2) {
            barcodeList.add(barcode(strList[i], strList[i + 1].toFloat()))
        }

        return barcodeList
    }
}

2. You can create a new entity (DB table) in your database for your barcode entities, and create a one to many relationship between your ArticoliServer and barcode entities. You can check this page for more info about defining relationship with Room persistence library.

P.S I recommend you to implement the second approach in case you have many barcode objects to persist in DB.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...