TableLayout with SQLite

huangapple go评论77阅读模式
英文:

TableLayout with SQLite

问题

我需要从SQLite中在tabletLayout中显示数据。SQLite的响应是正确的,数据也是正确的,我正确地添加了它们,但它不会将它们列出。它只显示一个:

TableLAyout.xml:

...

Kotlin代码:

private fun llenartabla() {
    val tableLayout = findViewById<TableLayout>(R.id.tlMarcado)
    val obraS = SharedApp.prefs.obra
    val obraSt = obraS.toString()

    val dbHelper = DBHandler(this)
    val db = dbHelper.readableDatabase

    val cursor: Cursor = db.rawQuery("select distinct dstarjeta, insumo, cantidad, precio_insumo, lote, manzana, id_detalle_kontrol from ActividadesM where obra = '" + obraSt + "'", null)

    cursor.moveToFirst()
    do{
        val a = TableRow(this)
        a.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)

        val tvCodigo = tableLayout.findViewById<View>(R.id.tvDescML) as TextView
        tvCodigo.text = cursor.getString(0)
        if (tvCodigo.parent != null) {
            (tvCodigo.parent as ViewGroup).removeView(tvCodigo) // <- 修复
        }
        a.addView(tvCodigo)

        val tvDescripcion = tableLayout.findViewById<View>(R.id.tvTarjetaML) as TextView
        tvDescripcion.text = cursor.getString(6)
        if (tvDescripcion.parent != null) {
            (tvDescripcion.parent as ViewGroup).removeView(tvDescripcion) // <- 修复
        }
        a.addView(tvDescripcion)

        val tvPrecio = tableLayout.findViewById<View>(R.id.tvPrecioML) as TextView
        tvPrecio.text = cursor.getString(2)
        if (tvPrecio.parent != null) {
            (tvPrecio.parent as ViewGroup).removeView(tvPrecio) // <- 修复
        }
        a.addView(tvPrecio)

        tableLayout.addView(a)
    }while (cursor.moveToNext())
}
英文:

I need to display data in tabletLayout from SQLite. The SQLite response is correct, the data is correct, I add them correct but it does not list them. It shows me only one :

TableLAyout.xml :

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.Views.MarcadoXLoteActivity&quot;&gt;
&lt;Spinner
android:id=&quot;@+id/spinner3&quot;
android:layout_width=&quot;105dp&quot;
android:background=&quot;@drawable/green_border&quot;
android:layout_height=&quot;36dp&quot;
android:layout_marginTop=&quot;8dp&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/textView73&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView73&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/imageView26x&quot;
android:layout_width=&quot;33dp&quot;
android:layout_height=&quot;46dp&quot;
android:layout_marginStart=&quot;-34dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;@+id/spinner3&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/spinner3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/spinner3&quot;
app:srcCompat=&quot;@drawable/baseline_arrow_drop_down_24&quot; /&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbarM&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:background=&quot;@color/white&quot;
android:minHeight=&quot;?attr/actionBarSize&quot;
android:theme=&quot;?attr/actionBarTheme&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:subtitleTextColor=&quot;@color/white&quot;
app:title=&quot;  Marcado de Avances Por Lote&quot;
app:titleTextAppearance=&quot;@style/TextAppearance.AppCompat.Display1&quot;
app:titleTextColor=&quot;@color/black&quot;&gt;
&lt;/androidx.appcompat.widget.Toolbar&gt;
&lt;androidx.cardview.widget.CardView
android:id=&quot;@+id/cardView2&quot;
android:layout_width=&quot;798dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;1dp&quot;
android:layout_marginTop=&quot;32dp&quot;
android:layout_marginEnd=&quot;1dp&quot;
app:cardBackgroundColor=&quot;#DEF1DE&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/toolbarM&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tvNomMarcado&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:text=&quot;     CIMENTACION&quot;
android:textColor=&quot;#11B818&quot;
android:textSize=&quot;24sp&quot;
android:textStyle=&quot;bold|italic&quot; /&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;TextView
android:id=&quot;@+id/textView72&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;80dp&quot;
android:layout_marginTop=&quot;24dp&quot;
android:text=&quot;Manzana&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;16sp&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/cardView2&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/cardView2&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView73&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;24dp&quot;
android:layout_marginEnd=&quot;220dp&quot;
android:text=&quot;Rango de Lotes&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;16sp&quot;
app:layout_constraintEnd_toEndOf=&quot;@+id/cardView2&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/cardView2&quot; /&gt;
&lt;Spinner
android:id=&quot;@+id/spinnerMan&quot;
android:layout_width=&quot;206dp&quot;
android:layout_height=&quot;36dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:background=&quot;@drawable/green_border&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/textView72&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView72&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/imageView26&quot;
android:layout_width=&quot;33dp&quot;
android:layout_height=&quot;46dp&quot;
android:layout_marginStart=&quot;-34dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;@+id/spinnerMan&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/spinnerMan&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/spinnerMan&quot;
app:srcCompat=&quot;@drawable/baseline_arrow_drop_down_24&quot; /&gt;
&lt;TableLayout
android:id=&quot;@+id/tlMarcado&quot;
android:layout_width=&quot;700dp&quot;
android:layout_height=&quot;789dp&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/spinner3&quot;&gt;
&lt;TableRow
android:background=&quot;#51B435&quot;
android:padding=&quot;10dp&quot;&gt;
&lt;TextView
android:layout_width=&quot;120dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:gravity=&quot;center&quot;
android:text=&quot;DESCRIPCI&#211;N&quot;
android:textColor=&quot;@color/white&quot; /&gt;
&lt;TextView
android:layout_width=&quot;78dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_weight=&quot;0.5&quot;
android:gravity=&quot;center&quot;
android:singleLine=&quot;false&quot;
android:text=&quot;PRECIO
CONTRATISTA&quot;
android:textColor=&quot;@color/white&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tvTarjetaML&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;36dp&quot;
android:layout_gravity=&quot;center&quot;
android:layout_weight=&quot;0.5&quot;
android:gravity=&quot;center&quot;
android:text=&quot;015004001&quot;
android:textColor=&quot;@color/white&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;36dp&quot;
android:layout_gravity=&quot;center&quot;
android:layout_weight=&quot;0.5&quot;
android:gravity=&quot;center&quot;
android:text=&quot;015004001&quot;
android:textColor=&quot;@color/white&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;36dp&quot;
android:layout_gravity=&quot;center&quot;
android:layout_weight=&quot;0.5&quot;
android:gravity=&quot;center&quot;
android:text=&quot;015004001&quot;
android:textColor=&quot;@color/white&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;36dp&quot;
android:layout_gravity=&quot;center&quot;
android:layout_weight=&quot;0.5&quot;
android:gravity=&quot;center&quot;
android:text=&quot;015004001&quot;
android:textColor=&quot;@color/white&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;36dp&quot;
android:layout_gravity=&quot;center&quot;
android:layout_weight=&quot;0.5&quot;
android:gravity=&quot;center&quot;
android:text=&quot;015004001&quot;
android:textColor=&quot;@color/white&quot; /&gt;
&lt;/TableRow&gt;
&lt;TableRow
android:background=&quot;#F0F7F7&quot;
android:padding=&quot;5dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tvDescML&quot;
android:layout_width=&quot;120dp&quot;
android:layout_height=&quot;55dp&quot;
android:layout_gravity=&quot;center&quot;
android:background=&quot;@drawable/tableborder&quot;
android:gravity=&quot;center&quot;
android:text=&quot;FAB. DE REGISTRO PLUBIAL&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tvPrecioML&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;60dp&quot;
android:layout_gravity=&quot;center&quot;
android:layout_weight=&quot;1&quot;
android:background=&quot;@drawable/tableborder&quot;
android:gravity=&quot;center&quot;
android:text=&quot; $129.00&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;60dp&quot;
android:layout_weight=&quot;1&quot;
android:background=&quot;@color/blueM&quot;
android:padding=&quot;6dp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;60dp&quot;
android:layout_weight=&quot;1&quot;
android:background=&quot;@color/blueM&quot;
android:padding=&quot;6dp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;60dp&quot;
android:layout_weight=&quot;1&quot;
android:background=&quot;@color/blueM&quot;
android:padding=&quot;6dp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;60dp&quot;
android:layout_weight=&quot;1&quot;
android:background=&quot;@color/blueM&quot;
android:padding=&quot;6dp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;60dp&quot;
android:layout_weight=&quot;1&quot;
android:background=&quot;#2A42C6&quot;
android:padding=&quot;6dp&quot; /&gt;
&lt;/TableRow&gt;
&lt;TableRow
android:background=&quot;#F0F7F7&quot;
android:padding=&quot;5dp&quot;/&gt;
&lt;TableRow
android:background=&quot;#F0F7F7&quot;
android:padding=&quot;6dp&quot;/&gt;
&lt;TableRow
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;TableRow
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;TableRow
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;TableRow
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;/TableLayout&gt;
&lt;Button
android:id=&quot;@+id/btnguardarmercado&quot;
android:layout_width=&quot;562dp&quot;
android:layout_height=&quot;72dp&quot;
android:layout_marginStart=&quot;352dp&quot;
android:layout_marginTop=&quot;30dp&quot;
android:layout_marginEnd=&quot;352dp&quot;
android:background=&quot;@drawable/btn_gray&quot;
android:text=&quot;GUARDAR AVANCE&quot;
android:textSize=&quot;30sp&quot;
app:backgroundTint=&quot;@null&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/tlMarcado&quot; /&gt;
&lt;Spinner
android:id=&quot;@+id/spinner4&quot;
android:layout_width=&quot;105dp&quot;
android:layout_height=&quot;36dp&quot;
android:layout_marginStart=&quot;16dp&quot;
android:background=&quot;@drawable/green_border&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/textView71&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/spinner3&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/imageView88&quot;
android:layout_width=&quot;33dp&quot;
android:layout_height=&quot;46dp&quot;
android:layout_marginStart=&quot;-34dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;@+id/spinner4&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/spinner4&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/spinner4&quot;
app:srcCompat=&quot;@drawable/baseline_arrow_drop_down_24&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView71&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;7dp&quot;
android:text=&quot;Al&quot;
android:textColor=&quot;#040404&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;@+id/spinner3&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/spinner3&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView73&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Kotlin code:

 private fun llenartabla() {
val tableLayout = findViewById&lt;TableLayout&gt;(R.id.tlMarcado)
val obraS = SharedApp.prefs.obra
val obraSt = obraS.toString()
val dbHelper = DBHandler(this)
val db = dbHelper.readableDatabase
val cursor: Cursor = db.rawQuery(&quot;select distinct dstarjeta, insumo, cantidad, precio_insumo, lote, manzana, id_detalle_kontrol from ActividadesM where obra = &#39;&quot; + obraSt + &quot;&#39;&quot;, null)
cursor.moveToFirst()
do{
val a = TableRow(this)
a.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)
val tvCodigo = tableLayout.findViewById&lt;View&gt;(R.id.tvDescML) as TextView
tvCodigo.text = cursor.getString(0)
if (tvCodigo.getParent() != null) {
(tvCodigo.getParent() as ViewGroup).removeView(tvCodigo) // &lt;- fix
}
a.addView(tvCodigo)
val tvDescripcion = tableLayout.findViewById&lt;View&gt;(R.id.tvTarjetaML) as TextView
tvDescripcion.text = cursor.getString(6)
if (tvDescripcion.getParent() != null) {
(tvDescripcion.getParent() as ViewGroup).removeView(tvDescripcion) // &lt;- fix
}
a.addView(tvDescripcion)
val tvPrecio = tableLayout.findViewById&lt;View&gt;(R.id.tvPrecioML) as TextView
tvPrecio.text = cursor.getString(2)
if (tvPrecio.getParent() != null) {
(tvPrecio.getParent() as ViewGroup).removeView(tvPrecio) // &lt;- fix
}
a.addView(tvPrecio)
tableLayout.addView(a)
}while (cursor.moveToNext())    }

I can't get it to work:

TableLayout with SQLite

答案1

得分: 2

我还建议使用“RecyclerView”。
但是如果你需要使用TableLayout,请考虑以下:

我创建了一个基于你的问题的示例应用程序。

a)将表格TableLayout更新如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".sobounty.SoBountctivity">
    <TableLayout
        android:id="@+id/test_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:background="#51B435"
            android:padding="10dp">

            <TextView
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="DESCRIPCIÓN"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="78dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:singleLine="false"
                android:text="PRECIO CONTRATISTA"
                android:textColor="@color/white" />

            <TextView
                android:id="@+id/tvTarjetaML"
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="015004001"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="015004001"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="015004001"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="015004001"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="015004001"
                android:textColor="@color/white" />
        </TableRow>
    </TableLayout>
</ScrollView>

我将它作为布局使用,因此我将ScrollView用作根容器。

b)创建第二个布局文件(table_row_layout),如下所示(将你的样式等添加到其中,我删除了一些内容以使事情变得简单)。这包含了行视图。

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvDescML"
        android:layout_width="120dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="FAB. DE REGISTRO PLUBIAL" />

    <TextView
        android:id="@+id/tvPrecioML"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text=" $129.00" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:padding="6dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:padding="6dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:padding="6dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:padding="6dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:background="#2A42C6"
        android:padding="6dp" />
</TableRow>

c)活动代码(根据需要调整逻辑)。在这里,我们将填充table_row_layout,然后将其添加到viewgroup中。

class SoBountctivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_so_bountctivity)
        // 我们要插入视图的tableLayout
        val tableLayout = findViewById<TableLayout>(R.id.test_table)
        // 假数据
        val fruitList = returnFruitList()
        // 布局充气器
        val layoutInflater = layoutInflater

        // 你的光标可以替代forEach。循环遍历数据
        fruitList.forEach {

            // 获取新的行布局
            val view = layoutInflater.inflate(R.layout.table_row_layout, tableLayout, false)
            // 获取你想要添加数据的视图
            val textView = view.findViewById<TextView>(R.id.tvDescML)
            // 设置数据
            textView.text = it
            // 将行视图添加到tableLayout中
            tableLayout.addView(view)
        }


        for (rowIndex in 0 until tableLayout.childCount) {
            val rowView: TableRow = tableLayout.getChildAt(rowIndex) as TableRow
            for (colIndex in 0 until rowView.childCount) {
                val view = rowView.getChildAt(colIndex)
                view.setOnClickListener {
                    // 对于监听器。
                    onTableCellClicked(rowIndex, colIndex)

                }
            }
        }
    }

    private fun returnFruitList() = listOf("Apple", "Banana", "Pineapple", "Guava", "Berries", "JackFruit", "Tomato", "Papaya")

    private fun onTableCellClicked

<details>
<summary>英文:</summary>

I would also recommend to use &quot;RecyclerView&quot;.
But in case if you need to use the tablelayout, please consider following 

I created a sample app based on your issue.

a) Update the table TableLayout as follows 

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    
    &lt;ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    tools:context=&quot;.sobounty.SoBountctivity&quot;&gt;
    &lt;TableLayout
        android:id=&quot;@+id/test_table&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;
        &lt;TableRow
            android:background=&quot;#51B435&quot;
            android:padding=&quot;10dp&quot;&gt;

            &lt;TextView
                android:layout_width=&quot;120dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;DESCRIPCI&#211;N&quot;
                android:textColor=&quot;@color/white&quot; /&gt;

            &lt;TextView
                android:layout_width=&quot;78dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_weight=&quot;0.5&quot;
                android:gravity=&quot;center&quot;
                android:singleLine=&quot;false&quot;
                android:text=&quot;PRECIO


            CONTRATISTA&quot;
                android:textColor=&quot;@color/white&quot; /&gt;

            &lt;TextView
                android:id=&quot;@+id/tvTarjetaML&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;36dp&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_weight=&quot;0.5&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;015004001&quot;
                android:textColor=&quot;@color/white&quot; /&gt;

            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;36dp&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_weight=&quot;0.5&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;015004001&quot;
                android:textColor=&quot;@color/white&quot; /&gt;

            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;36dp&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_weight=&quot;0.5&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;015004001&quot;
                android:textColor=&quot;@color/white&quot; /&gt;

            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;36dp&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_weight=&quot;0.5&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;015004001&quot;
                android:textColor=&quot;@color/white&quot; /&gt;

            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;36dp&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_weight=&quot;0.5&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;015004001&quot;
                android:textColor=&quot;@color/white&quot; /&gt;
        &lt;/TableRow&gt;


    &lt;/TableLayout&gt;

&lt;/ScrollView&gt;

I am using it as layout so, I used ScrollView as root container.

b) Create a second layout file (table_row_layout)as follows (add your styles etc in it, I have removed few of them to make things simple). This contains the row views. 

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;TableRow xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;
    &lt;TextView
        android:id=&quot;@+id/tvDescML&quot;
        android:layout_width=&quot;120dp&quot;
        android:layout_height=&quot;55dp&quot;
        android:layout_gravity=&quot;center&quot;
        android:gravity=&quot;center&quot;
        android:text=&quot;FAB. DE REGISTRO PLUBIAL&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/tvPrecioML&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;60dp&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_weight=&quot;1&quot;
        android:gravity=&quot;center&quot;
        android:text=&quot; $129.00&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;60dp&quot;
        android:layout_weight=&quot;1&quot;
        android:padding=&quot;6dp&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;60dp&quot;
        android:layout_weight=&quot;1&quot;
        android:padding=&quot;6dp&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;60dp&quot;
        android:layout_weight=&quot;1&quot;
        android:padding=&quot;6dp&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;60dp&quot;
        android:layout_weight=&quot;1&quot;
        android:padding=&quot;6dp&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;60dp&quot;
        android:layout_weight=&quot;1&quot;
        android:background=&quot;#2A42C6&quot;
        android:padding=&quot;6dp&quot; /&gt;
     &lt;/TableRow&gt;

c) Activity code (Adjust the logic as per your need) Here, we will inflate the table_row_layout and then add it to the viewgroup 

    class SoBountctivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_so_bountctivity)
        // tableLayout i which we want to insert view
        val tableLayout = findViewById&lt;TableLayout&gt;(R.id.test_table)
        // Fake data
        val fruitList = returnFruitList()
        // layoutInflater
        val layoutInflater = layoutInflater

        // Your cursor can replace the forEach. Looping over the data
        fruitList.forEach {

            // Get the new row_layout
            val view = layoutInflater.inflate(R.layout.table_row_layout, tableLayout, false)
            // Get the views in which you want to add the data
            val textView = view.findViewById&lt;TextView&gt;(R.id.tvDescML)
            // Set the data
            textView.text = it
            // Add the row view in tableLayout
            tableLayout.addView(view)
        }


        for (rowIndex in 0 until tableLayout.childCount) {
            val rowView: TableRow = tableLayout.getChildAt(rowIndex) as TableRow
            for (colIndex in 0 until rowView.childCount) {
                val view = rowView.getChildAt(colIndex)
                view.setOnClickListener {
                    // For listeners.
                    onTableCellClicked(rowIndex, colIndex)

                }
            }
        }
    }

    private fun returnFruitList() = listOf(&quot;Apple&quot;, &quot;Banana&quot;, &quot;Pineapple&quot;, &quot;Guava&quot;, &quot;Berries&quot;, &quot;JackFruit&quot;, &quot;Tomato&quot;, &quot;Papaya&quot;)

    private fun onTableCellClicked(rowIndex: Int, colIndex: Int) {
        Toast.makeText(this, &quot;I AM CLICKED row $rowIndex col $colIndex&quot;, Toast.LENGTH_SHORT).show()
    }
    }

&gt; However I too would take note of the comment left by @marcpetitvecino on the question.
Using RecyclerView is a better approach, as it is very flexible, memory efficient, has smooth scrolling as it recycles / reuses views and is very modular / customisable.

Result :
[![Sample][1]][1]


[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/YfECi.png
  [2]: https://i.stack.imgur.com/x1yVR.png

</details>



# 答案2
**得分**: 1

1. 问题出在代码如何访问TableLayout中的TextView上
2. 代码尝试通过它们的ID在TableLayout视图中找到TextViews
3. 删除if条件和后续的removeView()调用
4. 使代码更具性能

注意使用RecyclerView是更好的方法因为它非常灵活内存效率高具有平滑滚动因为它回收/重用视图并且非常模块化/可定制

<details>
<summary>英文:</summary>

**1. The problem with the code lies in how it is accessing the TextViews within the TableLayout.**
-
This is a simplified look at the TableLayout.xml which shows the following structure:
```xml
&lt;TableLayout
  android:id=&quot;@+id/tlMarcado&quot;
  .....&gt;
  &lt;TableRow
     .....&gt;
     &lt;TextView
       android:id=&quot;@+id/tvTarjetaML&quot;
       ...../&gt;
  &lt;/TableRow&gt;
&lt;/TableLayout&gt;

The TextViews with IDs (tvDescML, tvTarjetaML, tvPrecioML) are not direct children of TableLayout. They are in fact children of TableRow within TableLayout

2. The code is attempting to source the TextViews by their IDs within the TableLayout view:

val tvCodigo = tableLayout.findViewById&lt;View&gt;(R.id.tvDescML) as TextView
tvCodigo.text = cursor.getString(0)
if (tvCodigo.getParent() != null) {
(tvCodigo.getParent() as ViewGroup).removeView(tvCodigo) // &lt;- fix
}
a.addView(tvCodigo)

To resolve this issue, the code needs to find the TableRow first, then find the TextView within that TableRow:

val a = TableRow(this)
a.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)
val tvCodigo = a.findViewById&lt;TextView&gt;(R.id.tvDescML)
tvCodigo.text = cursor.getString(0)
if (tvCodigo.getParent() != null) {
(tvCodigo.getParent() as ViewGroup).removeView(tvCodigo) // &lt;- fix
}
a.addView(tvCodigo)

3. Remove the 'if' condition and the subsequent removeView() call

This code checks if the tvCodigo TextView has a parent view and removes it:

if (tvCodigo.getParent() != null) {
(tvCodigo.getParent() as ViewGroup).removeView(tvCodigo) // &lt;- fix
}

The context provided above is to add the tvCodigo TextView to a new TableRow and add that TableRow to the TableLayout. The removeView() method is used to remove a view from its parent view hierarchy.

However, since the code is creating a new TableRow (a) and the tvCodigo TextView is set as the child, there is no possibility for the TextView to have an existing parent. This code is unnecessary and should be removed, this will not affect the functionality of the code and will make it cleaner:

val a = TableRow(this)
a.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)
val tvCodigo = a.findViewById&lt;TextView&gt;(R.id.tvDescML)
tvCodigo.text = cursor.getString(0)
a.addView(tvCodigo)

4. Make the Code more Performant

The addView() method is called multiple times, modifying the view hierarchy frequently can have a performance impact. So only apply the method to the TableLayout at the end of the do block :

cursor.moveToFirst()
do{
val a = TableRow(this)
a.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)
val tvCodigo = a.findViewById&lt;TextView&gt;(R.id.tvDescML)
tvCodigo.text = cursor.getString(0)
val tvDescripcion = a.findViewById&lt;TextView&gt;(R.id.tvTarjetaML)
tvDescripcion.text = cursor.getString(6)
val tvPrecio = a.findViewById&lt;TextView&gt;(R.id.tvPrecioML)
tvPrecio.text = cursor.getString(2)
tableLayout.addView(a)
} while (cursor.moveToNext())

> This answers the question, however I would take note of the comment left by @marcpetitvecino on the question.
> Using RecyclerView is a better approach, as it is very flexible, memory efficient, has smooth scrolling as it recycles / reuses views and is very modular / customisable.

huangapple
  • 本文由 发表于 2023年6月19日 09:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定