提问人:Erdem 提问时间:11/17/2023 更新时间:11/17/2023 访问量:27
Jetpack Compose:LazyColumn 项目和过滤器不起作用
Jetpack Compose: LazyColumn items and filter not working
问:
我有一个 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。
错误是:致命异常:主
进程:com.erdemkaya.mybudgetizer,PID:
20501
java.lang.IndexOutOfBoundsException:索引 3,大小 3
答:
0赞
Erdem
11/17/2023
#1
这是 Material3 版本的问题。我使用的是 Material3 alpha 版本,但恢复到稳定版,现在它可以工作了
评论