英文:
Jet Pack Compose Mutable List Updating With Mutable State list Problem
问题
// 在 Activity 中声明
val stateBilledProducts = viewModel.listBilledProducts
// 在 ViewModel 中声明
class OrderViewModel : ViewModel() {
private val _listBilledProducts = mutableStateListOf<BilledProducts>()
val listBilledProducts: List<BilledProducts> = _listBilledProducts
fun QuantityIncrement(index: Int) {
viewModelScope.launch {
_listBilledProducts[index].quantity = _listBilledProducts.value[index].quantity + 1
_listBilledProducts[index].productPrice = _listBilledProducts.value[index].unitPrice * _listBilledProducts.value[index].quantity
}
}
fun QuantityDecrement(index: Int) {
viewModelScope.launch {
_listBilledProducts[index].quantity += 1
_listBilledProducts[index].productPrice = _listBilledProducts.value[index].unitPrice * _listBilledProducts.value[index].quantity
}
}
fun productRemove(index: Int) {
viewModelScope.launch {
_listBilledProducts.removeAt(index)
}
}
}
注意:这些代码段中只是翻译了注释以外的代码部分,没有进行其他额外的翻译或回答。
英文:
//declared in activity
val statebilledproducts = viewModel.listBilledProducts
//declared in viewmodel
class OrderViewModel : ViewModel() {
private val _listBilledProducts = mutableStateListOf<BilledProducts>()
val listBilledProducts :List<BilledProducts> = _listBilledProducts
fun Quantityincrement(index:Int){
viewModelScope.launch {
_listBilledProducts[index].quantity=_listBilledProducts.value[index].quantity+1
_listBilledProducts[index].productprice = _listBilledProducts.value[index].unitprice * _listBilledProducts.value[index].quantity
}
}
fun QuantityDecrement(index:Int){
viewModelScope.launch {
_listBilledProducts[index].quantity+=1
_listBilledProducts[index].productprice = _listBilledProducts.value[index].unitprice * _listBilledProducts.value[index].quantity
}
}
fun productRemove(index:Int){
viewModelScope.launch {
_listBilledProducts.removeAt(index)
}
}
}
i tried but state of jet pack Compose is not observing
答案1
得分: 3
mutableStateListOf
使列表本身可观察,而不是它的内容。换句话说,如果列表发生更改,比如更改、插入或删除项目,Compose 将收到通知,而不是实例,比如BilledProducts
,发生更改。
考虑使用data class
而不是可变类来表示BilledProducts
。例如,
data class BilledProduct(
val quantity: Int,
val unitPrice: Double
) {
val productPrice get() = quantity * unitPrice
}
然后,增加数量可以这样做,
fun QuantityIncrement(index:Int) {
viewModelScope.launch {
val lastBilled = _lastBilledProduces[index]
_listBilledProducts[index] = lastBilled.copy(quantity = lastBilled.quantity + 1)
}
}
英文:
mutableStateListOf
makes the list, not its content, observable. In other words, Compose will be notified if the list changes such as when items are changed, inserted, or deleted, not when the instances, such as the BilledProducts
, are changed.
Consider using an data class
for BilledProducts
instead of a mutable class. For example,
data class BilledProduct(
val quantity: Int,
val unitPrice: Double
) {
val productPrice get() = quantity * unitPrice
}
then incrementing the quantity would be,
fun QuantityIncrement(index:Int) {
viewModelScope.launch {
val lastBilled = _lastBilledProduces[index]
_listBilledProducts[index] = lastBilled.copy(quantity = lastBilled.quantity + 1)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论