Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当我试图在Kotlin中绘制动态按钮时,为什么我的foreach会中断?_Android_Kotlin - Fatal编程技术网

Android 当我试图在Kotlin中绘制动态按钮时,为什么我的foreach会中断?

Android 当我试图在Kotlin中绘制动态按钮时,为什么我的foreach会中断?,android,kotlin,Android,Kotlin,我需要在foreach循环中绘制动态按钮,该循环从anko sqlite检索数据,foreach只输入一次并中断,在布局中只绘制一个按钮,我做错了什么?我的代码是: fun loadZones (ctx: Context, update: String, view: View, layout: LinearLayout) { val zonesParser = rowParser{idzone: Int, zone: String -> Pair(idzone, zone)}

我需要在foreach循环中绘制动态按钮,该循环从anko sqlite检索数据,foreach只输入一次并中断,在布局中只绘制一个按钮,我做错了什么?我的代码是:

fun loadZones (ctx: Context, update: String, view: View, layout: LinearLayout) {
    val zonesParser = rowParser{idzone: Int, zone: String -> Pair(idzone, zone)}
    for (it in ctx.database.use {
        select("tableplan")
            .distinct()
            .column("idzone")
            .column("zone")
            .orderBy("zone")
            .parseList(zonesParser)
    }) {
        val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        val btnZone = layoutInflater.inflate(R.layout.zones_item, null) as MaterialButton
        btnZone.text = it.second
        btnZone.id = it.first
        layout.addView(btnZone, layoutParams)
        Log.e("PAIR", "FIN DEL CICLO")
        continue
    }
}
(2, LARRY)
(1, MADISON)
从我的查询中检索的数据如下:

fun loadZones (ctx: Context, update: String, view: View, layout: LinearLayout) {
    val zonesParser = rowParser{idzone: Int, zone: String -> Pair(idzone, zone)}
    for (it in ctx.database.use {
        select("tableplan")
            .distinct()
            .column("idzone")
            .column("zone")
            .orderBy("zone")
            .parseList(zonesParser)
    }) {
        val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        val btnZone = layoutInflater.inflate(R.layout.zones_item, null) as MaterialButton
        btnZone.text = it.second
        btnZone.id = it.first
        layout.addView(btnZone, layoutParams)
        Log.e("PAIR", "FIN DEL CICLO")
        continue
    }
}
(2, LARRY)
(1, MADISON)
这是我的活动,我需要在“lytZonesButtons”id中绘制按钮

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent" tools:context=".TablePlanFragment">
    <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent"
                                                     android:layout_height="wrap_content" android:elevation="2dp"
                                                     tools:targetApi="lollipop" app:liftOnScroll="true">
        <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbarTablePlan"
                style="@style/com.madison.Toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/table_title_module">
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>
    <LinearLayout
            android:layout_marginTop="56dp"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:orientation="horizontal"
            android:background="@color/orangeLighter"
            android:gravity="center_vertical"
            android:padding="5dp" android:id="@+id/lytZonesButtons" />
    <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="112dp"
            android:padding="5dp">
        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rc_tableplan"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

        />
    </androidx.core.widget.NestedScrollView>
</FrameLayout>

非常感谢所有试图帮助我的人,请一些管理员关闭我的问题。

提示仅显示一个按钮。你试图在同一地点将同一视图充气两次

您需要在xml中添加一个空的linearlayout。在你的循环中改变按钮

var btnZone = findViewById(R.layout.btnZone) 
button.text = "Pair"
btnZone.addView(button, layoutParams)
这不是确切的代码(甚至可能不是正确的语法),但它向您展示了如何修改循环

基本上,您是在尝试膨胀同一视图的同一实例。当你真的没有用这种方式膨胀任何视图时,你只需要添加视图

注意 如果在xml中添加另一个按钮视图时有一个linearlayout,它将在它下面添加它。如果将布局方向设置为水平,则按钮视图将添加到另一个按钮视图的旁边

下面是一个例子的链接。
很抱歉,我会确保我的代码与您的代码和变量的语法匹配,但我正在工作。

我不知道为什么会有“continue”关键字。你能删除它然后再试一次吗?你试过把从查询中检索到的数据分配给一个变量并在for循环之前检查它的长度吗?@crumy是的,我在尝试后放上了“continue”,我想也许用“continue”可以修复它,但它仍然是一样的。@Ricky Mo,是的,我打印检索到的数据,并显示两条记录,如果我退出MaterialButton实例,循环会抛出两条记录。如果你将for循环更改为
for(它在列表中(成对(2,“LARRY”)、成对(1,“MADISON”))
?@Vahalaru,我已经在线性布局中添加了按钮,“zones\u项”是我添加到线性布局中的按钮模板,哈哈,如果是蛇的话,它会咬住你的。让我把电脑拿出来,拿出笔和纸。你在这里搞得一团糟。如果你能准确地解释你想做什么。看起来你试图在屏幕顶部设置一行按钮,以便在添加按钮时滚动浏览?@Vahalaru Mmmm。。。类似于您所解释的,我需要根据从anko db检索到的数据绘制一组按钮,我放在anko db中的数据来自Web服务,我使用数据并将其保存在本地db中,每个按钮(称为区域按钮)的计划是在我的回收器视图中为每个按钮显示一组项目。当做