kotlin Jetpack Compose:LazyColumn项目和过滤器不工作

zd287kbt  于 2023-11-21  发布在  Kotlin
关注(0)|答案(1)|浏览(171)

我有一个LazyColumn,我传递了一个列表和一个过滤器到项目,它崩溃与IndexOutOfBoundException。任何想法可能是什么:

items(state.categoriesWithSubs.filter {
   it.categories.type == tabState
}.takeIf { it.isNotEmpty() } ?: emptyList()) { categoriesWithSubs ->
   val filteredBalances =
    categoriesWithSubs.balances.filter { balances ->
        balances.month == 11
    }.takeIf { it.isNotEmpty() } ?: emptyList()

   val balanceValue =
    if (filteredBalances.isNotEmpty()) filteredBalances.first().balance else 0.0

   Card(modifier = Modifier
    .fillMaxWidth()
    .padding(horizontal = 16.dp), shape = RoundedCornerShape(20)) {
    Column(Modifier.fillMaxWidth()) {
        Row(
            Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp)
                .padding(top = 32.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                bitmap = ImageBitmap.imageResource(
                    StoredIcons.asRes(
                        categoriesWithSubs.categories.icon
                    )
                ),
                contentDescription = "Category Image",
            )
            Spacer(modifier = Modifier.width(8.dp))
            Column(verticalArrangement = Arrangement.Center) {
                Text(
                    text = categoriesWithSubs.categories.name,
                    style = MaterialTheme.typography.titleLarge,
                    fontWeight = FontWeight.Bold
                )
                Text(
                    text = getCorrectCurrencyFormat(amount = balanceValue),
                    style = MaterialTheme.typography.titleMedium,
                    color = if (isSystemInDarkTheme()) colorResource(id = R.color.darkonback) else colorResource(
                        id = R.color.lightonback
                    ),
                    fontWeight = FontWeight.Bold
                )
            }
        }
        Spacer(modifier = Modifier.height(16.dp))
        val progressFloat =
            if (balanceValue > 0 && totalSumBalancesPerMonth > 0) {
                (balanceValue / totalSumBalancesPerMonth).toFloat()
            } else 0.0f
        LinearProgressBar(
            progress = progressFloat,
            modifier = Modifier
                .fillMaxWidth()
                .height(32.dp)
                .padding(horizontal = 16.dp)
                .clip(RoundedCornerShape(50))
        )
        Spacer(modifier = Modifier.height(16.dp))
        HorizontalDivider(
            modifier = Modifier.padding(horizontal = 16.dp),
            thickness = 1.dp
        )
        Spacer(modifier = Modifier.height(8.dp))
        Text(
            text = "${(progressFloat * 100).toBigDecimal().setScale(2, RoundingMode.HALF_UP)}% der Gesamtausgaben entfallen auf diese Kategorie",
            modifier = Modifier.padding(horizontal = 16.dp),
            color = if (isSystemInDarkTheme()) colorResource(id = R.color.darkonback) else colorResource(
                id = R.color.lightonback
            ), fontWeight = FontWeight.Bold,
            textAlign = TextAlign.Center
        )
        Spacer(modifier = Modifier.height(16.dp))
        if(categoriesWithSubs.subcategories.isNotEmpty()) {
            categoriesWithSubs.subcategories.forEach { subCats ->
                val filteredSub =
                    state.subcategoriesWithCategories.filter { subCats ->
                        subCats.subcategories.catId == categoriesWithSubs.categories.id
                    }.takeIf { it.isNotEmpty() } ?: emptyList()

                val filteredSubBalances = if (filteredSub.isNotEmpty()) {
                    filteredSub[0].balancesSubs.filter { balSub ->
                        balSub.month == 11
                    }
                } else emptyList()

                val filteredSubBalance =
                    if (filteredSubBalances.isNotEmpty()) filteredSubBalances.first().balance else 0.0

                Row(
                    modifier = Modifier.fillMaxSize(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center
                ) {
                    Image(
                        bitmap = ImageBitmap.imageResource(
                            StoredIconsSub.asRes(
                                subCats.icon
                            )
                        ),
                        contentDescription = "SubCategory Image",
                        modifier = Modifier
                            .padding(
                                start = 16.dp,
                                end = 8.dp,
                                top = 8.dp
                            )
                            .size(24.dp)
                    )
                    Text(
                        text = "${subCats.name}: ",
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier
                            .fillMaxHeight()
                            .padding(top = 8.dp)
                    )
                    Text(
                        text = getCorrectCurrencyFormat(amount = filteredSubBalance),
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier
                            .fillMaxHeight()
                            .padding(top = 8.dp),
                        color = if (isSystemInDarkTheme()) colorResource(id = R.color.darkonback) else colorResource(
                            id = R.color.lightonback
                        )
                    )
                }
            }
        }
        Spacer(modifier = Modifier.height(32.dp))
    }
}
}

字符串
我有两个索引为0或1的选项卡,数据库中有14个类型为0的条目和1个类型为1的条目。
错误为:
致命异常:main
过程:com.erdemkaya. myquitizer,PID:20501
java.lang.IndexOutOfBoundsException:索引3,大小3

rjzwgtxy

rjzwgtxy1#

这是Material3版本的一个问题。我使用的是Material3 alpha版本,但回到稳定,现在它的工作

相关问题