Android 当我向房间数据库插入内容时,Kotlin 自定义 BottomSheetDialog 自动打开

Android Kotlin custom BottomSheetDialog automatically open when I insert something to Room Database

在我的自定义 BottomSheetDialog 中,我做了一个弹出窗口来注册一些东西。但是当我将数据从对话框插入房间数据库时,BottomSheetDialog 再次出现在屏幕元素的顶部!但是我没有写任何代码来重新打开 BottomSheetDialog!!

实体与道在此class:

类别2实体

@Entity(tableName = "categories_2")
data class Category2Entity(

@PrimaryKey
@ColumnInfo(name = "id")
val id: Int = 0,

@ColumnInfo(name = "name")
val name: String = "",

@ColumnInfo(name = "image")
val image: String = ""
)

Category2Dao

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(category2Entity: Category2Entity)

插入方式

binding.btnAddInput.setOnClickListener {
    val category2Dao = (activity.application as UserApp).db.category2Dao()

    lifecycleScope.launch {
        category2Dao.insert(Category2Entity(id, name, "ic_fish"))

        Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show()
    }
}

这次插入数据后,BottomSheetDialog在每个对话框的顶部重新打开..!!


-> 之后我把自己注册的数据插入到另一个Entity和Dao中 class 来解决问题,插入数据后没有出现 BottomSheetDialog再次!!

实体与道在此class

类别实体

@Entity(tableName = "categories")
data class CategoryEntity(

@PrimaryKey
@ColumnInfo(name = "id")
val id: Int = 0,

@ColumnInfo(name = "name")
val name: String = "",

@ColumnInfo(name = "image")
val image: String = ""
)

CategoryDao

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(categoryEntity: CategoryEntity)

插入方式

binding.btnAddInput.setOnClickListener {
val categoryDao = (activity.application as UserApp).db.categoryDao()

lifecycleScope.launch {
    categoryDao.insert(CategoryEntity(id, name, "ic_fish"))

    Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show()
}

}

这次插入数据后,上面没有出现BottomSheetDialog..!!


现在我的问题是,为什么在向Category2Entity插入数据时,BottomSheetDialog出现在最上面?

[注意:我没有写任何代码来再次显示 BottomSheetDialog。我的数据插入也适用于两个实体。只是Entity2重新打开BottomSheetDialog]


BottomSheetDialog 函数:

 private fun sheetDialog() {
     val inputSheet = BottomSheetDialog(context, R.style.Bottom_Sheet)
     val binding = SheetInputBinding.inflate(layoutInflater)
     inputSheet.setContentView(binding.root)

     showDialog()

     inputSheet.show()

}

对话函数:

 private fun showDialog() {
     val addDialog = Dialog(context, R.style.Theme_Dialog)
     val binding = DialogAddMoreBinding.inflate(layoutInflater)
     addDialog.setContentView(binding.root)

     binding.btnAddInput.setOnClickListener {
     val category2Dao = (activity.application as UserApp).db.category2Dao()

     lifecycleScope.launch { category2Dao.insert(Category2Entity(id, name, "ic_fish"))

     Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show()
     }
     
     addDialog.show()

}

我已经通过 re-creating 整个项目解决了这个问题。刚刚将所有代码复制并粘贴到新项目中,现在没有问题了!! 也许我的项目文件有一些问题,或者可能是鬼!! :-(