【问题标题】:IndexOutOfBoundsException in AdapterListAdapterList 中的 IndexOutOfBoundsException
【发布时间】:2020-03-04 21:16:20
【问题描述】:

我尝试在最近展示的AdapterList 组件中组织处理动态数据的工作。作为测试,我将数据列表置于父组件的状态,并添加了一个按钮,每次点击都会将初始列表的长度减少一个。

val (data, setData) = state { testFilms }

Column {
    Button(onClick = {
        setData(testFilms.dropLast(1))
    }) {
        Text("Change")
    }

    AdapterList(
        data,
        modifier = LayoutPadding(5.dp) + LayoutHeight.Fill + LayoutWidth.Fill
    ) { film ->
        FilmItemView(
            film = film,
            selectFilmAction = selectFilmAction,
            logger = logger
        )
    }
}

但我在重构AdapterList 时收到以下错误,而不是预期的结果:

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.get(ArrayList.java:437)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3$1.invoke(AdapterList.kt:448)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3$1.invoke(Unknown Source:0)
    at androidx.compose.ObserveKt.Observe(Observe.kt:37)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3.invoke(Unknown Source:11)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3.invoke(Unknown Source:0)
    at androidx.compose.Recomposer$recompose$1.invoke(Recomposer.kt:61)
    at androidx.compose.Recomposer$recompose$1.invoke(Recomposer.kt:19)
    at androidx.compose.ViewComposerKt.runWithCurrent(ViewComposer.kt:387)
    at androidx.compose.Recomposer.recompose(Recomposer.kt:51)
    at androidx.compose.Recomposer.access$recompose(Recomposer.kt:19)
    at androidx.compose.Recomposer$Companion.recompose$compose_runtime_release(Recomposer.kt:42)
    at androidx.compose.Composition.compose(Composition.kt:67)
    at androidx.compose.Composition.compose(Composition.kt:59)
    at androidx.compose.Compose$subcomposeInto$1.invoke(Compose.kt:253)
    at androidx.compose.Compose$subcomposeInto$1.invoke(Compose.kt:23)
    at androidx.compose.ViewComposerKt.runWithComposing(ViewComposer.kt:397)
    at androidx.compose.Compose.subcomposeInto(Compose.kt:252)
    at androidx.ui.foundation.ListState.composeChildForDataIndex-N7Qnm20(AdapterList.kt:447)
    at androidx.ui.foundation.ListState.recomposeAllChildren(AdapterList.kt:358)
    at androidx.ui.foundation.ListState.recomposeIfAttached(AdapterList.kt:352)
    at androidx.ui.foundation.AdapterListKt.AdapterList(AdapterList.kt:486)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2$invoke$1.invoke(FilmListView.kt:174)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2$invoke$1.invoke(Unknown Source:0)
    at androidx.compose.ObserveKt.Observe(Observe.kt:37)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2.invoke(Unknown Source:20)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2.invoke(Unknown Source:3)

有谁知道使用此组件处理动态数据的任何方法?

【问题讨论】:

  • 这看起来像是 AdapterList 中的一个错误,我会尽快研究一下。我想我看到了问题。

标签: android kotlin android-jetpack-compose


【解决方案1】:

如果您是最新的 dev06 Jetpack Compose,您有两种方法可以做到这一点:

  • 您的List<Any> 必须是“撰写感知”并且当前可用的是ModelList<Any>
  • 您可以使用 @Model 标记创建模型,因为它们会更改状态,并且您的 @Compososable 函数将知道(使用 MutableList

示例代码:

fun addLogic(modelList: ModelList<MyModel>) {
  modelList.add(MyModel("Smith John", 10))
}

class MyModel(var name: String, var index: Int)

@Composable
fun RecycledList() { // Any name you want
  val modelList<MyModel> = modelListOf()
  var counter = 0

  addLogic(modelList)

  modelList.add(MyModel("John Doe", 99))

  MaterialTheme {
    Column {
      Container(height = 70.dp) {
         Align(alignment = Alignment.Center) {
            Button(onClick = { 
               modelList.add(MyModel("John Smith", counter++))
               // Any other logic you want
            }) {
               Text("ADD ITEM")
            }
         }
      }
      AdapterList(data = modelList) { item ->
         Center {
            Text("Hello ${item.name} - Index: ${item.index}")
         }
      },
    }
  }
}

结果:

在测试AdapterList 时,我注意到IndexOutOfBoundsException 在删除值时但在添加时没有问题。收到回复后,我会更新我的答案。

【讨论】:

    【解决方案2】:

    这是一个错误 (issue 153195921),将在即将发布的版本中由 this patch 修复。

    快速浏览一下,您的代码应该可以使用该补丁,尽管我无法从示例中对其进行测试。不过,它与补丁中的测试用例非常相似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多