簡體   English   中英

LazyColumn 通知項目的修改

[英]LazyColumn notify about modification of item

我有一個看起來像這樣的可組合函數:


@Composable
fun listScreen(context: Context, owner: ViewModelStoreOwner) {
    val repository = xRepository(getAppDatabase(context).xDao()
    val listData by repository.readAllData.observeAsState(emptyList())
    // repository.readAllData returns LiveData<List<xEntity>>
   // listData is a List<xEntity>
    
    LazyColumn(){ 
        items(listData.size) {
            Card {
                  Text(listData[it].name)
                  Text(listData[it].hoursLeft.toString())
                  Button(onClick = {updateInDatabase(owner, name = listData[it], hoursLeft = 12)}) {...}
                  }
        }
    }

}

fun updateInDatabase(owner: ViewModelStoreOwner, name: String, hoursLeft: Int) {
     val xViewModel....
     val newEntity = xEntity(name=name, hoursLeft = Int)
     xViewModel.update(newEntity)
}

正如您可能猜到的那樣,LazyColumn 在修改實體后不會刷新,有沒有辦法在每次更新實體后更新 listData?

編輯:

class xRepository(private val xDatabaseDao) {
    val readAllData: LiveData<List<xEntity>> = xDatabaseDao.getallXinfo()
    ...
    suspend fun updatePlant(x: xEntity) {
        plantzDao.updateX(x)
    }
}

interface xDatabaseDao {
    @Query("SELECT * FROM xInfo ORDER BY id DESC")
    fun getAllXInfo(): LiveData<List<xEntity>>
    ....
    
    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun updateX(x: xEntity?)
}

實體修改:


fun updatePlantInDatabase(owner: ViewModelStoreOwner, name: String, waterAtHour: Int, selectedDays: ArrayList<Int>) {
    val xViewModel: xViewModel = ViewModelProvider(owner).get(xViewModel::class.java)
    val new = xEntity(name = name, waterAtHour = waterAtHour, selectedDays = selectedDays)
    xViewModel.updatePlant(new)
}

我使用mutableStateOf來包裝需要重組的字段。

class TestColumnEntity(
    val id: String,
    title: String = ""
){
    var title: String by mutableStateOf(title)
}

看法:

        setContent {
            val mData = mutableStateListOf(
                TestColumnEntity("id_0").apply { title = "cnm"},
                TestColumnEntity("id_1").apply { title = "cnm"},
                TestColumnEntity("id_2").apply { title = "cnm"},
                TestColumnEntity("id_3").apply { title = "cnm"},
                TestColumnEntity("id_4").apply { title = "cnm"},
                TestColumnEntity("id_5").apply { title = "cnm"},
            )
            Column {
                Button(onClick = {
                    mData.add(TestColumnEntity("id_${Random.nextInt(100) + 6}").apply { title = "ccnm" })
                }) {
                    Text(text = "add data")
                }
                Button(onClick = {
                    mData[1].title = "test_${Random.nextInt(100)}"
                }) {
                    Text(text = "update data")
                }
                TestLazyColumn(data = mData, key = {index, item ->
                    item.id
                }) {
                    Text(text = it.title)
                }
            }
        }

它適用於我的測試用例

如果您想更新惰性列(比如在 jetpack compose 中重新組合),請使用副作用。 當列表更改副作用時,將列表獲取功能置於副作用(啟動效果或其他副作用)中,自動重組您的功能並顯示更新的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM