房间数据库android的问题

时间:2020-09-14 18:42:02

标签: android database kotlin mvvm

我想将我从交易中获得的一些价值插入到Room数据库中。

我使用Insert方法将数据保存到会议室数据库中,还使用@query从数据库中获取所有数据。

但是问题是数据没有保存,当我在屏幕之间切换时,没有数据的迹象。

代码:

我的桌子

@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)


}

Viewmodel Room:


    // 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()
    }

}

这是我插入和获取数据的活动:


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()


                    }

                })
}

谢谢帮助。

1 个答案:

答案 0 :(得分:1)

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