简体   繁体   English

从惰性列列表中删除

[英]Removed From the lazy column list

Whenever I try to remove from the lazy column list I get arrayIndexOutOfBoundException This is the array that每当我尝试从惰性列列表中删除时,我都会得到arrayIndexOutOfBoundException This is the array that

var productsList = remember { mutableStateListOf<Product>() }//I load products in this list

Whenever the user presses a certain button I do the following每当用户按下某个按钮时,我都会执行以下操作

productsList.remove(item) 

I get array arrayIndexOutOfBoundException this is how I loop as well我得到数组arrayIndexOutOfBoundException这也是我循环的方式

itemsIndexed(productsList) { index, item ->
        

Anyway to avoid that error无论如何要避免那个错误

Whole code for those interested:感兴趣的人的完整代码:

fun MyProducts(navController: NavController,myProductsViewModel: MyProductsViewModel= viewModel()) {
    var productsList = remember { mutableStateListOf<Product>() }
    val scope = rememberCoroutineScope()

    val listState = rememberLazyListState()

    var currentImage = remember { mutableStateListOf<Int>() }

    LaunchedEffect(key1 = Unit){
       myProductsViewModel.getShop()
        productsList.addAll(myProductsViewModel.productsList)
        currentImage.addAll(List(productsList.size) {0})

    }
    var pickedImage: MutableState<String?> =remember { mutableStateOf("") }


    BackHandler() {
        navController.popBackStack()
    }
       LazyColumn(
           Modifier
               .fillMaxSize()
               .padding(start = 16.dp),
            horizontalAlignment = Alignment.Start,
            verticalArrangement = Arrangement.spacedBy(8.dp)
            ,state = listState
        ) {
           itemsIndexed(productsList) { index, item ->
                                   .clickable {when(icon){
                                       Icons.Default.Delete->{
                                           scope.launch {
                                               myProductsViewModel.removeProducts(item.product_id,item.shop_id,item)
                                            productsList.remove(item)
                                           }
                                       }
                                       Icons.Default.Edit->{

                                       }

I am accessing the same list in the rest of the code but I don't think it is relevant to the problem我正在访问代码 rest 中的相同列表,但我认为它与问题无关

You have not provided a proper code example but it sounds like you are not updating the list and the lazy column properly so the item is removed from the list but the lazy column does not know about it.您没有提供正确的代码示例,但听起来您没有正确更新列表和惰性列,因此该项目已从列表中删除,但惰性列不知道它。

From my point of view you are generally having some bad practice in your code.从我的角度来看,您的代码通常有一些不好的做法。 For example:例如:

LaunchedEffect(key1 = Unit){
   myProductsViewModel.getShop()
    productsList.addAll(myProductsViewModel.productsList)
    currentImage.addAll(List(productsList.size) {0})
}

This all looks like stuff that should be done in the viewmodel.这一切看起来都像是应该在视图模型中完成的事情。 Generally a composable is supposed to convert state to UI, that means, it shouldnt contain any business logic.通常,可组合项应该将 state 转换为 UI,这意味着它不应包含任何业务逻辑。 The code snipped I just showed looks like something that can be done in the viewmodel.我刚刚展示的代码片段看起来像是可以在视图模型中完成的事情。

Having something like this有这样的事情

var productsList = remember { mutableStateListOf<Product>() }

and then adding the elements with a LaunchedEffect is not how composables are supposed to be used.然后添加带有 LaunchedEffect 的元素并不是可组合项的应有使用方式。

First your myProductsViewModel.productsList should be a LiveData object thats holding your product list.首先,您的myProductsViewModel.productsList应该是一个 LiveData object,它包含您的产品列表。 Then you are supposed to do the following:那么你应该做以下事情:

val productList by myProductsViewModel.productsList.observeAsState(emptyList())

Then you show it in your composable.然后在可组合项中显示它。 If you want to change the list content, you should call a method for that on the viewmodel, which then updates the livedata object of your list accordingly.如果你想改变列表内容,你应该在视图模型上调用一个方法,然后相应地更新列表的实时数据 object 。

I you hope I explained in clearly enough.我希望我解释得足够清楚。 Let me know if you have questions.如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM