Android 安卓房间插入未插入且不执行任何操作

Android 安卓房间插入未插入且不执行任何操作,android,android-room,dagger-hilt,Android,Android Room,Dagger Hilt,我不熟悉android和room。我使用Dagger-Hilt注入数据库,但当我使用Insert方法时,什么也没有发生 实体: @Entity(tableName = "product_table") @Parcelize data class Product( @PrimaryKey @SerializedName("id") val id : String, @SerializedName("title&q

我不熟悉android和room。我使用Dagger-Hilt注入数据库,但当我使用Insert方法时,什么也没有发生

实体:


@Entity(tableName = "product_table")
@Parcelize
data class Product(

    @PrimaryKey
    @SerializedName("id")
    val id : String,
    @SerializedName("title")
    val title : String,
    @SerializedName("price")
    val price : String,
    @SerializedName("category")
    val category : String,
    @SerializedName("description")
    val description : String,
    @SerializedName("image")
    val image : String,

    val color : String,
    val size : String
): Parcelable
道:

匕首柄模块:


@Module
@InstallIn(SingletonComponent::class)
object MainActivityModule {

    // Retrofit code

    @Provides
    @Singleton
    fun provideDatabase(
        app: Application,
    ) = Room.databaseBuilder(
        app,
        ProductDatabase::class.java,
        "product_db").build()

    @Provides
    fun provideTaskDao(db: ProductDatabase) = db.productDao()


}

片段视图模型:


@HiltViewModel
class ProductViewModel @Inject constructor(
    private val productDao : CartDao
) : ViewModel() {

    init {

    }


    fun insert_item_to_cart(product: Product){
        viewModelScope.launch{
            productDao.insert_item_to_cart(product)
        }

    }
}
单击侦听器上的片段按钮:

btn_add_to_cart.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {

                val num = viewModel.insert_item_to_cart(
                    args.productData.copy(
                        color = selected_color,
                        size = selected_size
                    )
                )
                Log.d(TAG, "Inserted : ${num}")

            }
        })

由于我的Insert函数返回
Long
,这将是插入产品的主键,因此此日志应该返回该数字,但它根本不返回任何内容。希望有人能帮助我并感谢您的关注。

您是否100%确定正在呼叫
将物品插入购物车
?是的,我确定。我在
insert\u item\u to\u cart
中有一些导航功能,一切正常@HenryTwistOh,我刚刚看到您打印了
将项目插入购物车的退货
,但它没有退货类型?因此,它绝对不应该打印您期望的号码。你可以在Android Studio上用数据库检查器检查数据是否已经插入。哦,我检查了一下,结果发现没有数据库连接。那么我的匕首柄模块现在的问题是什么@HenryTwistWell如果您没有遇到崩溃或编译器错误,那么它显然是被注入的,所以看起来不太可能。您是否尝试记录insert查询的返回值?

@HiltViewModel
class ProductViewModel @Inject constructor(
    private val productDao : CartDao
) : ViewModel() {

    init {

    }


    fun insert_item_to_cart(product: Product){
        viewModelScope.launch{
            productDao.insert_item_to_cart(product)
        }

    }
}
btn_add_to_cart.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {

                val num = viewModel.insert_item_to_cart(
                    args.productData.copy(
                        color = selected_color,
                        size = selected_size
                    )
                )
                Log.d(TAG, "Inserted : ${num}")

            }
        })