Jet Pack Compose中可变列表更新与可变状态列表问题

huangapple go评论56阅读模式
英文:

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&lt;BilledProducts&gt;()
val listBilledProducts :List&lt;BilledProducts&gt; = _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)
    }
}

huangapple
  • 本文由 发表于 2023年3月7日 01:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定