繁体   English   中英

android kotlin:将检索到的数据从 url 存储到房间数据库

[英]android kotlin: storing retrieved data from url to room database

我做的方法:

    private fun saveToRoom(albums: Array<Data>) {

    doAsync {
        /*val currentDBPath = getDatabasePath("albums_database").absolutePath
        println("DBPath is " + currentDBPath)*/

        var items = ArrayList<ProductList_Data>()

        for (album in albums) {
            val item = ProductList_Data()
            item.name = album.name
            item.price = album.price
            items.add(item)

        }

        db?.productListDao()?.insert(items)

        /*val musicAlbums = db?.productListDao()?.getAll()
        activityUiThread {
            longToast("Data Got saved")
            refreshUIWith(musicAlbums!!)
        }*/
    }

}

这是数据类名数据

data class Data(
val _links: Links?,
val attributes: List<Attribute>?,
val average_rating: String?,
val backordered: Boolean?,
val backorders: String?,
val backorders_allowed: Boolean?,
val button_text: String?,
val catalog_visibility: String?,
val categories: List<Category>?,
val cross_sell_ids: List<Any>?,
val custom_fields: CustomFields?,
val date_created: String?,
val date_created_gmt: String?,
val date_modified: String?,
val date_modified_gmt: String?,
val date_on_sale_from: Any?,
val date_on_sale_from_gmt: Any?,
val date_on_sale_to: Any?,
val date_on_sale_to_gmt: Any?,
val default_attributes: List<Any>?,
val description: String?,
val dimensions: Dimensions?,
val download_expiry: Int?,
val download_limit: Int?,
val downloadable: Boolean?,
val downloads: List<Any>?,
val external_url: String?,
val featured: Boolean?,
val grouped_products: List<Any>?,
val id: Int?,
val images: List<Image>?,
val manage_stock: Boolean?,
val menu_order: Int?,
val meta_data: List<MetaData>?,
val name: String?,
val on_sale: Boolean?,
val parent_id: Int?,
val permalink: String?,
val price: String?,
val price_html: String?,
val purchasable: Boolean?,
val purchase_note: String?,
val rating_count: Int?,
val regular_price: String?,
val related_ids: List<Int>?,
val reviews_allowed: Boolean?,
val sale_price: String?,
val shipping_class: String?,
val shipping_class_id: Int?,
val shipping_required: Boolean?,
val shipping_taxable: Boolean?,
val short_description: String?,
val sku: String?,
val slug: String?,
val sold_individually: Boolean?,
val status: String?,
val stock_quantity: Any?,
val stock_status: String?,
val tags: List<Any>?,
val tax_class: String?,
val tax_status: String?,
val total_sales: Int?,
val type: String?,
val upsell_ids: List<Any>?,
val variations: List<Int>?,
val virtual: Boolean?,
val weight: String?

) 我只想将名称和价格存储在房间数据库中

我正在使用的实体类:

@Entity(tableName = "productlisttable")
data class ProductList_Data(
@PrimaryKey
val uid: Int = 0,

@ColumnInfo(name = "_name")
var name: String? = "",

@ColumnInfo(name = "_price")
var price: String? = ""

)

在主活动中调用方法并获得红色下划线错误,即没有为 init 传递值以及所有这些错误

        saveToRoom(Array<Data>))

我认为我调用该方法的方式是错误的或什么

使用此函数将数组转换并保存在数据库中

@TypeConverter
    fun fromStringList(stringList: List<String?>?): String? {
        if (stringList == null) return null
        val type = object : TypeToken<List<String?>?>() {}.type
        return Gson().toJson(stringList, type)
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM