会议室数据库:插入的ID始终为0 [英] Room database: inserted ID is always 0

查看:92
本文介绍了会议室数据库:插入的ID始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含列表项的Room数据库.我在这里遵循示例: https://medium.com/mindorks /room-kotlin-android-architecture-components-71cad5a1bb35

I am trying to create a Room database of list items. I am following the example here: https://medium.com/mindorks/room-kotlin-android-architecture-components-71cad5a1bb35

这是我的数据类:

@Entity(tableName = "itemData")
data class ItemData(@PrimaryKey(autoGenerate = true) var id: Long? = null,
                    @ColumnInfo(name = "text") var text: String,
                    @ColumnInfo(name = "checked") var checked: Boolean

)

DAO:

@Dao
interface ItemDataDAO {

    @Insert(onConflict = REPLACE)
    fun insert(itemData: ItemData) : Long
}

数据库:

@Database(entities = arrayOf(ItemData::class), version = 1)
abstract class ItemDatabase() : RoomDatabase() {


    abstract fun itemDataDao(): ItemDataDAO

    companion object {
        private var INSTANCE: ItemDatabase? = null

        fun getInstance(context: Context): ItemDatabase? {
            if (INSTANCE == null) {
                synchronized(ItemDatabase::class) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            ItemDatabase::class.java, "items.db")
                            .build()
                }
            }
            return INSTANCE
        }

        fun destroyInstance() {
            INSTANCE = null
        }
    }
}

工作线程:

class DbWorkerThread(threadName: String) : HandlerThread(threadName) {

    private lateinit var mWorkerHandler: Handler

    override fun onLooperPrepared() {
        super.onLooperPrepared()
        mWorkerHandler = Handler(looper)
    }

    fun postTask(task: Runnable) {
        mWorkerHandler.post(task)
    }
}

这里是侦听器,而不是调用插入函数:

Here is the listener than calls the insert function:

editText.setOnEditorActionListener { v, actionId, event ->
    return@setOnEditorActionListener when (actionId) {
        EditorInfo.IME_ACTION_DONE -> {
            createItem(editText)
            true
        }
        else -> false
    }
}

这是被调用以插入项目的函数:

Here is the function that gets called to insert the item:

fun createItem(editText: EditText) {
    var text = editText.text.toString().trim()
    var itemData = ItemData(text = text, checked = false)
    var id : Long? = 0
    val task = Runnable {
        id = mDb?.itemDataDao()?.insert(itemData)
        println("inserted $id")
    }
    mDbWorkerThread.postTask(task)
}

但是ID始终为0,因此只能插入一项.谁能看到这个问题?

But the ID is always 0 so only one item ever gets inserted. Can anyone see the problem?

我还尝试过删除id的默认null值,但结果相同.

I have also tried removing the default null value for the id, but it gives the same result.

推荐答案

以下更改解决了该问题.

The following changes fixed the issue.

而不是像这样调用构造函数

Instead of calling the constructor like this

var itemData = ItemData(text = text, checked = false)

我调用了默认构造函数,然后在构造后设置参数.

I called the default constructor and then set the parameters after construction.

var itemData = ItemData()
itemData.text = text
itemData.checked = false

这需要ItemData实体的以下构造函数:

This required the following constructor to the ItemData entity:

constructor():this(null,"",false)

这篇关于会议室数据库:插入的ID始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆