kotlin 为什么捕获的项目对象给我在编写中找不到引用

1cklez4t  于 2022-11-30  发布在  Kotlin
关注(0)|答案(1)|浏览(115)

我有一个数据类:

data class Feed_Status(val img:Int, val name_id: String)

我上课:

class Feed_helper {

    fun Image_getter(): List<() -> Feed_Status> {

        val Images = listOf {
            Feed_Status(R.drawable.image_demo1, "name1")
            Feed_Status(R.drawable.image_demo2, "name2")
            Feed_Status(R.drawable.image_demo3, "name3")
            Feed_Status(R.drawable.image_demo4, "name4")
            Feed_Status(R.drawable.image_demo5, "name5")
            Feed_Status(R.drawable.image_demo6, "name6")
            Feed_Status(R.drawable.image_demo7, "name7")
            Feed_Status(R.drawable.image_demo8, "name8")
            Feed_Status(R.drawable.image_demo9, "name9")
            Feed_Status(R.drawable.image_demo10, "name10")
            Feed_Status(R.drawable.image_demo11, "name11")
            Feed_Status(R.drawable.image_demo12, "name12")
            Feed_Status(R.drawable.image_demo13, "name13")
            Feed_Status(R.drawable.image_demo14, "name14")
            Feed_Status(R.drawable.image_demo15, "name15")
            Feed_Status(R.drawable.image_demo16, "name16")
            Feed_Status(R.drawable.image_demo17, "name17")
            Feed_Status(R.drawable.image_demo18, "name18")
            Feed_Status(R.drawable.image_demo19, "name19")
            Feed_Status(R.drawable.image_demo20, "name20")
            Feed_Status(R.drawable.image_demo21, "name21")
            Feed_Status(R.drawable.image_demo22, "name22")
            Feed_Status(R.drawable.image_demo23, "name23")
            Feed_Status(R.drawable.image_demo24, "name24")
            Feed_Status(R.drawable.image_demo25, "name25")
            Feed_Status(R.drawable.image_demo25, "name26")
        }

        return Images
    }
}

通过它我在lazyRow中调用items()

@Composable
fun feed() {

    LazyColumn(
        reverseLayout = false,
        modifier = Modifier
            .fillMaxSize(),
        userScrollEnabled = true
    ) {
          // Status(es)
         item {
            LazyRow(
                reverseLayout = false,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                userScrollEnabled = true
            ) {
                 val statuses = Feed_helper().Image_getter()
                 items(statuses) { status ->
                    Column(
                        verticalArrangement = Arrangement.Center,
                        modifier = Modifier
                                .width(80.dp)

                    ) {

                        Card(
                            shape = CircleShape,
                            modifier = Modifier
                                .padding(8.dp)
                                .size(64.dp)
                        ) {

                            Image(
                                painterResource(id = status.img),
                                contentDescription = status.name_id + "'s status",
                                contentScale = ContentScale.Crop
                            )

                        }

                        Text(
                            text = status.name_id,
                            modifier = Modifier.fillMaxWidth(),
                            textAlign = TextAlign.Center,
                        )

                    }

                }

            }

        }

    }

}

但是每当我在items()中从statusesstatuses调用元素时,它给我的是Reference Not found!
来电者:
Image()中的painterResource(id = status.img)
Image()中的contentDescription = status.name_id + "'s status"
Text中的text = status.name_id
所有呼叫者都在items(statuses){ status ->
我花了几个小时来解决这个问题。所以任何帮助都将非常感激。
如果你发现任何错字,请更新它或告诉我修复。
PS:这是我第一次来这里,我对Android开发和Kotlin几乎没有任何经验。我开发了终端应用程序,用Python、C++、C进行了ML工作。所以我可能需要更多的信息来解释。我一周前才开始学习Android开发。
编辑:你可以问我任何更多的信息。
平安

0yg35tkg

0yg35tkg1#

Image_getter的返回类型更改为List<Feed_status>,而不是lambda类型,并在声明大括号列表时将大括号更改为圆括号。

class Feed_helper {

    fun Image_getter(): List<Feed_Status> { // change it to this instead of type of lambda `() -> Feed_status`

        val Images = listOf ( // change it to this instead of braces
           ...
        )

        return Images
    }
}

以及导入了

import androidx.compose.foundation.lazy.items

相关问题