Android RecyclerView中的文件室数据库条目
我想在回收视图中显示房间数据库条目。到目前为止,我已经有了房间骨架,我可以在RecyclerView中显示一些虚拟内容(不是来自房间): 但是,我很难显示房间数据库条目,而不是虚拟内容。在Arduino中,EEPROM I/O几乎是一个单行程序,但在Android房间中,这项概念上简单的任务似乎是一项代码密集型任务,而不是向前挑战。这就引出了我的第一个问题: 1) 在我的例子中,数据库非常纤细和简单,有没有比使用更少开销和类的Room更简单的方法 关于房间的方法,我相信我已经非常接近了。我在实施以下方面遇到困难: 2) 如何用房间数据库条目(ViewModel中的所有跳转)替换init DummyContent中的for循环 以下是我到目前为止得到的信息(我没有在ViewModel下面发布任何内容,例如Repository和DAO,因为它现在应该不感兴趣): DummyItems(虚拟内容由房间数据库条目替换)Android RecyclerView中的文件室数据库条目,android,kotlin,android-recyclerview,android-room,android-database,Android,Kotlin,Android Recyclerview,Android Room,Android Database,我想在回收视图中显示房间数据库条目。到目前为止,我已经有了房间骨架,我可以在RecyclerView中显示一些虚拟内容(不是来自房间): 但是,我很难显示房间数据库条目,而不是虚拟内容。在Arduino中,EEPROM I/O几乎是一个单行程序,但在Android房间中,这项概念上简单的任务似乎是一项代码密集型任务,而不是向前挑战。这就引出了我的第一个问题: 1) 在我的例子中,数据库非常纤细和简单,有没有比使用更少开销和类的Room更简单的方法 关于房间的方法,我相信我已经非常接近了。我在实
object DummyContent{
//样本(虚拟)项的数组。
val项:MutableList=ArrayList()
//按ID显示的样本(虚拟)项目的地图。
val ITEM_MAP:MutableMap=HashMap()
私有val计数=25
初始化{
//添加一些示例项。
//要替换为房间数据库条目,您可以尝试将其添加到对象DummyContent
object DummyContent {
val jumpsLiveData = MutableLiveData<List<JumpData>>()
private val observedLiveData: LiveData<List<JumpData>>? = null
private val dataObserver = object : Observer<List<JumpData>> {
override fun onChanged(newList: List<JumpData>) {
// Do something with new data set
}
}
fun observeJumpsData(jumpsLiveData: LiveData<List<JumpData>>) {
observedLiveData?.removeObserver(dataObserver)
observedLiveData = jumpsLiveData.apply {
observeForever(dataObserver)
}
}
}
通过此代码,DummyContent
将在创建ViewModel
后自动订阅新数据
在“活动”中,您在其中创建了RecyclerView
,将此文本添加到onCreate
的末尾:
override fun onCreate(savedState: Bundle?) {
DummyContent.jumpsLiveData.observe(this, Observer {
recyclerAdapter.changeItemsList(it)
}
}
changeItemsList
-更改回收商数据的方法,我相信,您已经创建了它谢谢您的建议!到目前为止,回收视图仍然是空的,我想这是我让onChanged()保持原样的预期。但我仍在努力添加所有跳转(来自ViewModel)你能给我一行代码吗?为RecyclerView的适配器添加了设置内容
class JumpViewModel(application: Application) : AndroidViewModel(application) {
// The ViewModel maintains a reference to the repository to get data.
private val repository: JumpRepository
// LiveData gives us updated words when they change.
val allJumps: LiveData<List<JumpData>>
init {
// Gets reference to WordDao from WordRoomDatabase to construct
// the correct WordRepository.
val jumpsDao = JumpRoomDatabase.getDatabase(application, viewModelScope).jumpDao()
repository = JumpRepository(jumpsDao)
allJumps = repository.allJumps // OF INTEREST <----------------------------------------------------
}
fun insert(jump: JumpData) = viewModelScope.launch {
repository.insert(jump)
}
fun getJumps() : LiveData<List<JumpData>> {
return allJumps
}
}
object DummyContent {
val jumpsLiveData = MutableLiveData<List<JumpData>>()
private val observedLiveData: LiveData<List<JumpData>>? = null
private val dataObserver = object : Observer<List<JumpData>> {
override fun onChanged(newList: List<JumpData>) {
// Do something with new data set
}
}
fun observeJumpsData(jumpsLiveData: LiveData<List<JumpData>>) {
observedLiveData?.removeObserver(dataObserver)
observedLiveData = jumpsLiveData.apply {
observeForever(dataObserver)
}
}
}
init {
val jumpsDao = JumpRoomDatabase.getDatabase(application, viewModelScope).jumpDao()
repository = JumpRepository(jumpsDao)
allJumps = repository.allJumps
DummyContent.observeJumpsData(getJumps())
}
override fun onCreate(savedState: Bundle?) {
DummyContent.jumpsLiveData.observe(this, Observer {
recyclerAdapter.changeItemsList(it)
}
}