Android AdapterList中的IndexOutofBoundSexException

Android AdapterList中的IndexOutofBoundSexException,android,kotlin,android-jetpack-compose,Android,Kotlin,Android Jetpack Compose,我试图在最近发布的AdapterList组件中使用动态数据组织工作。作为测试,我将数据列表置于父组件的状态,并添加了一个按钮,每次单击都会将初始列表的长度减少一个 val (data, setData) = state { testFilms } Column { Button(onClick = { setData(testFilms.dropLast(1)) }) { Text("Change") } AdapterList

我试图在最近发布的
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
        )
    }
}
但是,在重新编译
适配器列表时,出现了以下错误,而不是预期的结果:

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)

有人知道使用此组件处理动态数据的方法吗?

如果您使用的是最新的dev06 Jetpack Compose,则有两种方法:

  • 您的
    列表

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

    这是一个bug(),它将在即将发布的版本中修复


    快速查看,您的代码应该可以使用该补丁,尽管我无法从示例中测试它。不过,它与补丁中的测试用例非常相似。

    这看起来像AdapterList中的一个bug,我将很快对此进行研究。我想我看到了问题所在。
    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}")
             }
          },
        }
      }
    }