Android(Kotlin)无法访问TableRow元素的子元素

问题描述 投票:1回答:1

我有一个以编程方式构建的TableLayout,工作正常。但是我无法访问TableRow元素的子元素。 row.getChildAt(x)导致“未解析的引用”

它从日志输出中看到该行实际上是TableRow类型,所以我不确定为什么这会失败。我是Kotlin和Android的新手,所以我正在寻找一些帮助。

我的问题是为什么在创建TableView时row.getChildAt可用,但是在迭代通过TableView时以后不可用?

package com.example.daedalus.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TableRow
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_test_table.*

class testTable : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_test_table)
    val number = 5

    for(i in 0 until number) {
        val row = TableRow(this)
        val cell1 = TextView(this)
        cell1.text = "Hello"
        val cell2 = TextView(this)
        cell2.text = "Friend"
        row.addView(cell1)
        row.addView(cell2)
        tableLayout.addView(row)
        // row.getChildAt(x) works here
    }
    val view = Button(this)
    view.text = "Test"
    view.setOnClickListener(){
        for(i in 0 until tableLayout.childCount) {
            val row = tableLayout.getChildAt(i)
            //row.getChildAt(x) doesnt work here. Unresolved reference
            Log.i("MyApp",row.toString())
        }
    }
    linearLayout.addView(view)
   }
}

XML:

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="24dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</TableLayout>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="0dp"
    android:layout_height="84dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="32dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tableLayout"></LinearLayout>
</android.support.constraint.ConstraintLayout>

Logcat输出

09-11 14:08:09.823 26050-26050/com.example.daedalus.myapplication I/MyApp: android.widget.TableRow{afec180 V.E...... ........ 0,0-954,51}
android.widget.TableRow{66d93b9 V.E...... ........ 0,51-954,102}
android.widget.TableRow{9c073fe V.E...... ........ 0,102-954,153}
android.widget.TableRow{ebcf75f V.E...... ........ 0,153-954,204}
09-11 14:08:09.824 26050-26050/com.example.daedalus.myapplication I/MyApp: android.widget.TableRow{c4e75ac V.E...... ........ 0,204-954,255}
android kotlin android-tablelayout tablerow
1个回答
1
投票

因为getChildAt(int)是ViewGroup声明的方法,TableLayout扩展了。如果你启用了类型提示,你应该看到row只是一个View对象。

由于TableLayout的子节点都应该是TableRow对象,所以只需转换:

val row = tableLayout.getChildAt(i) as TableRow
val item = row.getChildAt(whatever) //you may need to cast again
© www.soinside.com 2019 - 2024. All rights reserved.