【问题标题】:Android (Kotlin) cannot access child of TableRow elementAndroid(Kotlin)无法访问 TableRow 元素的子元素
【发布时间】:2018-09-11 21:11:04
【问题描述】:

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

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

我的问题是为什么 row.getChildAt 在创建 TableView 时可用,但在以后迭代 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】:

    因为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
    

    【讨论】:

    • 谢谢。这澄清了我遇到的其他一些问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2016-05-03
    • 2012-09-09
    相关资源
    最近更新 更多