gpt4 book ai didi

java - 如何在编辑文本中显示 Realm 数据库结果?

转载 作者:行者123 更新时间:2023-12-01 18:00:28 24 4
gpt4 key购买 nike

所以我是 Realm 数据库的新手,我试图显示用户编写的数据,抛出编辑文本并将其显示在 TextView 中。

我的 Realm 等级

     import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass

@RealmClass

open class GoodStudents : RealmObject(){
@PrimaryKey
var id: Long = 0
var name : String? = null
var grade : Int? = null


}

主要 Activity 代码

Realm.init(this)
val convig = RealmConfiguration.Builder()
.name("GoodStudents").build()
val realm = Realm.getInstance(convig)
realm.beginTransaction()

count = realm.where(GoodStudents::class.java).findAll().size

val goodStudents = realm.createObject(GoodStudents::class.java, count+1)
goodStudents.name = name.text.toString()
goodStudents.grade
realm.commitTransaction()


val readData = realm.where(GoodStudents::class.java).findAll()

saveButton.setOnClickListener {
Toast.makeText(this,"Data is saved", Toast.LENGTH_LONG).show()
var text = text.text
readData.forEach { save ->
save.name = text as String?


}

}

PC。 Java 代码是可以接受的..

最佳答案

所以我发现编写这段代码是可行的 saveButton.setOnClickListener {

        val convig = RealmConfiguration.Builder()
.name("GoodStudents").build()
val realm = Realm.getInstance(convig)
realm.beginTransaction()

count = realm.where(GoodStudents::class.java).findAll().size
val goodStudents = realm.createObject(GoodStudents::class.java, count+1)
goodStudents.name = name.text.toString()
goodStudents.grade = grade.text.toString().toInt()

val readData = realm.where(GoodStudents::class.java).findAll()

readData.forEach { save ->

text.text = "" + save.name + ": " + goodStudents.grade

}

realm.commitTransaction()

}

关于java - 如何在编辑文本中显示 Realm 数据库结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60643920/

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