gpt4 book ai didi

android - RecyclerView中的 session 室数据库条目

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

我想在 RecyclerView 中显示房间数据库条目。到目前为止,我已经有了Room骨架,并且可以在RecyclerView中显示一些虚拟内容(不是来自Room):

enter image description here

但是,我很难显示Room DB条目而不是虚拟内容。在Arduino中,EEPROM I / O东西曾经几乎是一个单行,但是在Android Room中,此概念上容易完成的任务似乎是代码密集而不是挑战性的挑战。这使我想到了第一个问题:

1)就我而言,数据库非常 slim 和简单,是否有比Room使用更少开销和更少类的简单方法?

关于“房间”方法,我认为我已经很接近了。我很难实现以下目标:

2)如何用Room-DB条目(ViewModel中的allJumps)替代init DummyContent中的for循环?

到目前为止,这是我得到的(我没有在ViewModel下发布任何东西,例如Repository和DAO,因为它现在不应该引起人们的兴趣):

DummyItems (虚拟内容由Room DB条目替换)

object DummyContent {

// An array of sample (dummy) items.
val ITEMS: MutableList<DummyItem> = ArrayList()

// A map of sample (dummy) items, by ID.
val ITEM_MAP: MutableMap<String, DummyItem> = HashMap()

private val COUNT = 25

init {
// Add some sample items.
// TO BE REPLACED BY ROOM DB ENTRIES <----------------------------------------------------
for (i in 1..COUNT) {
addItem(createDummyItem(i))
}
}

private fun addItem(item: DummyItem) {
ITEMS.add(item)
ITEM_MAP.put(item.id, item)
}

private fun createDummyItem(position: Int): DummyItem {
return DummyItem(position.toString(), "Item " + position, makeDetails(position))
}

private fun makeDetails(position: Int): String {
val builder = StringBuilder()
builder.append("Details about Item: ").append(position)
for (i in 0..position - 1) {
builder.append("\nMore details information here.")
}
return builder.toString()
}

// A dummy item representing a piece of content.
data class DummyItem(val id: String, val content: String, val details: String) {
override fun toString(): String = content
}
}

allJumps / JumpData
// allJumps is of type LiveData<List<JumpData>>

@Entity
data class JumpData (
@PrimaryKey var jumpNumber: Int,
var location: String?
}

ViewModel
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

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

这是viewModel的init块:
init {
val jumpsDao = JumpRoomDatabase.getDatabase(application, viewModelScope).jumpDao()
repository = JumpRepository(jumpsDao)
allJumps = repository.allJumps
DummyContent.observeJumpsData(getJumps())
}

通过此代码, DummyContent将在创建 ViewModel后自动订阅新数据

在创建 RecyclerView的“ Activity ”中,将此文本添加到 onCreate的末尾:
override fun onCreate(savedState: Bundle?) {
DummyContent.jumpsLiveData.observe(this, Observer {
recyclerAdapter.changeItemsList(it)
}
}
changeItemsList-更改回收者数据的方法,我相信您已经创建了它

关于android - RecyclerView中的 session 室数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60773102/

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