如何读取DataBaseRoom并显示textView [英] How to read DataBaseRoom and display a textView
问题描述
我正在使用dataBaseRoom进行练习,为此,我为他们创建了一个按钮,该按钮允许我读取数据库,我想输入用户名,并且在按下按钮(读取数据库)时会反映出我的名字,姓氏texview中的名称和年龄,问题是我不知道如何在textView中显示数据.
I am practicing with dataBaseRoom, for them I have created a button, which allow me to read database , i want to put the name of a user and when pressing the button (read data base) reflects me, his name, last name and age in the texview,the problem is that I do not know how to display the data in a textView.
我要执行操作的活动
//实体
@Entity(tableName = "user_table")
data class User(
@PrimaryKey(autoGenerate = true)@ColumnInfo(name = "id") val id: Int,
val firstName: String,
val lastName: String,
val age: Int
)
//Dao
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addUser(user: User)
@Query("SELECT * FROM user_table ORDER BY id ASC")
fun readAllData(): LiveData<User>
}
//数据库
@Database(entities = [User::class], version = 1, exportSchema = false)
abstract class UserDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile
private var INSTANCE: UserDatabase? = null
fun getDatabase(context: Context): UserDatabase{
val tempInstance = INSTANCE
if(tempInstance != null){
return tempInstance
}
synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
UserDatabase::class.java,
"user_database"
).build()
INSTANCE = instance
return instance
}
}
}
}
//存储库
class UserRepository(private val userDao: UserDao) {
val readAllData: LiveData<User> = userDao.readAllData()
suspend fun addUser(user: User){
userDao.addUser(user)
}
}
//ViewModel
//ViewModel
class UserViewModel(application: Application) : AndroidViewModel(application) {
val readAllData: LiveData<User>
private val repository: UserRepository
init {
val userDao = UserDatabase.getDatabase(application).userDao()
repository = UserRepository(userDao)
readAllData = repository.readAllData
}
fun addUser(user: User) {
viewModelScope.launch(Dispatchers.IO) {
repository.addUser(user)
}
}
}
我想在此类中添加我的texview
I want to add my texview in this class
mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
mUserViewModel.readAllData.observe(viewLifecycleOwner, Observer {
textView1.text = it.firstName
textView2.text = it.lastName
textView3.text = it.age.toString()
})
推荐答案
在您的片段中,将观察者添加到LiveData并在该观察者中设置TextView值.
In your fragment add an observer to the LiveData and set the TextView value within the observer.
要在字段中获取最新添加的用户的数据,可以使用它(检查 .size
或 .length
对您而言是否正确):>
To get the data of the latest added user in the fields, you can use this (check whether .size
or .length
is correct for you):
mUserViewModel.readAllData.observe(viewLifecycleOwner, Observer { users ->
if (users != null) {
for (user in users) { // I'm not sure about kotlin syntax
if (user.firstName == enteredFirstName &&
user.lastName == enteredLastname) {
textViewFirstName.text = user.firstName
textViewLastName.text = user.lastName
textViewAge.text = user.age.toString()
这篇关于如何读取DataBaseRoom并显示textView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!