gpt4 book ai didi

android - 房间数据库android的问题

转载 作者:行者123 更新时间:2023-12-02 12:49:42 24 4
gpt4 key购买 nike

我想将从交易中获得的一些值插入到 Room 数据库中。
我使用 Insert 方法将数据保存到房间数据库中,并使用 @query 从数据库中获取所有数据。
但问题是数据未保存,当我在屏幕之间切换时没有数据迹象。
代码 :
我的 table

@Entity (tableName = "credit")
data class Credit(

@PrimaryKey(autoGenerate = true)
val id : Int? ,
var credittext: Long

)
我的道:
        //credit table dao

@Query("SELECT * FROM credit ")
fun creditall () : LiveData<Credit>


@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertcredit (model : Credit)


@Query("DELETE FROM credit")
suspend fun deletecredit ()


}
我的存储库:
    // repository for Credit

fun getallcredit() = db.GetDao().creditall()

suspend fun deletallcredit() = db.GetDao().deletecredit()

suspend fun insertcredit(model : Credit) = db.GetDao().insertcredit(model)


}
观景房:

// this is for credit tb


fun creditall() = repository.getallcredit()


fun deletecredit() = CoroutineScope(Dispatchers.IO).launch {


repository.deletallcredit()

}


fun insertcredit(model: Credit) = CoroutineScope(Dispatchers.IO).launch {


repository.insertcredit(model)


}
我的房间数据库:
package com.example.ahwazfriut.Room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase


@Database(entities = [RoomTables::class , Credit::class], version = 1, exportSchema = false)


abstract class DataBaseRoom : RoomDatabase() {

abstract fun GetDao(): DaoCart


companion object {
@Volatile
private var instance: DataBaseRoom? = null

private val lock = Any()

operator fun invoke(context: Context) = instance
?: synchronized(lock) {
instance
?: makeDatabase(
context
).also {
instance = it
}
}

private fun makeDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
DataBaseRoom::class.java,
"name"
).build()
}

}
这是我插入和获取数据的 Activity :

class Payment_Activity : AppCompatActivity() {

lateinit var viewmodel: ViewModelRoom

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.payment_activity)


paymentVerification()

}
private fun paymentVerification() {


val textmoney: TextView = findViewById(R.id.money)
val data: Uri? = intent.data

val getpurchase = ZarinPal.getPurchase(this)


getpurchase.verificationPayment(data) {

isPaymentSuccess, refID, paymentRequest ->

if (isPaymentSuccess) {


val database = DataBaseRoom(this)
val repositoryCart = RepositoryCart(database)
val factoryRoom = FactoryRoom(repositoryCart)

viewmodel = ViewModelProvider(ViewModelStoreOwner { ViewModelStore() } , factoryRoom).get(ViewModelRoom::class.java)

viewmodel.insertcredit(Credit(null , paymentRequest.amount))

viewmodel.creditall().observe(this, Observer {

if (it != null) {

textmoney.text = it.credittext.toString()


}

})
}
感谢帮助 。

最佳答案

问题是我在错误的地方声明了 getallcredit 方法。实际上它必须在 on create View 上存储数据。

关于android - 房间数据库android的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63890133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com