使用SQLite的TableLayout

x6yk4ghg  于 2023-06-23  发布在  SQLite
关注(0)|答案(1)|浏览(73)

bounty将在5天内到期。回答此问题可获得+100声望奖励。MexiCano正在寻找一个答案从一个有信誉的来源

我需要从SQLite在tabletLayout中显示数据。SQLite响应是正确的,数据是正确的,我添加它们是正确的,但它没有列出它们。它只显示了一个:
TableLAyout.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Views.MarcadoXLoteActivity">

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="105dp"
    android:background="@drawable/green_border"
    android:layout_height="36dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="@+id/textView73"
    app:layout_constraintTop_toBottomOf="@+id/textView73" />

<ImageView
    android:id="@+id/imageView26x"
    android:layout_width="33dp"
    android:layout_height="46dp"
    android:layout_marginStart="-34dp"
    app:layout_constraintBottom_toBottomOf="@+id/spinner3"
    app:layout_constraintStart_toEndOf="@+id/spinner3"
    app:layout_constraintTop_toTopOf="@+id/spinner3"
    app:srcCompat="@drawable/baseline_arrow_drop_down_24" />

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbarM"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:subtitleTextColor="@color/white"
    app:title="  Marcado de Avances Por Lote"
    app:titleTextAppearance="@style/TextAppearance.AppCompat.Display1"
    app:titleTextColor="@color/black">

</androidx.appcompat.widget.Toolbar>

<androidx.cardview.widget.CardView
    android:id="@+id/cardView2"
    android:layout_width="798dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="1dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="1dp"
    app:cardBackgroundColor="#DEF1DE"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbarM">

    <TextView
        android:id="@+id/tvNomMarcado"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="     CIMENTACION"
        android:textColor="#11B818"
        android:textSize="24sp"
        android:textStyle="bold|italic" />

</androidx.cardview.widget.CardView>

<TextView
    android:id="@+id/textView72"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="80dp"
    android:layout_marginTop="24dp"
    android:text="Manzana"
    android:textColor="#000000"
    android:textSize="16sp"
    app:layout_constraintStart_toStartOf="@+id/cardView2"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<TextView
    android:id="@+id/textView73"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="220dp"
    android:text="Rango de Lotes"
    android:textColor="#000000"
    android:textSize="16sp"
    app:layout_constraintEnd_toEndOf="@+id/cardView2"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<Spinner
    android:id="@+id/spinnerMan"
    android:layout_width="206dp"
    android:layout_height="36dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/green_border"
    app:layout_constraintStart_toStartOf="@+id/textView72"
    app:layout_constraintTop_toBottomOf="@+id/textView72" />

<ImageView
    android:id="@+id/imageView26"
    android:layout_width="33dp"
    android:layout_height="46dp"
    android:layout_marginStart="-34dp"
    app:layout_constraintBottom_toBottomOf="@+id/spinnerMan"
    app:layout_constraintStart_toEndOf="@+id/spinnerMan"
    app:layout_constraintTop_toTopOf="@+id/spinnerMan"
    app:srcCompat="@drawable/baseline_arrow_drop_down_24" />

<TableLayout
    android:id="@+id/tlMarcado"
    android:layout_width="700dp"
    android:layout_height="789dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/spinner3">

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

    <TableRow
        android:background="#F0F7F7"
        android:padding="5dp">

        <TextView
            android:id="@+id/tvDescML"
            android:layout_width="120dp"
            android:layout_height="55dp"
            android:layout_gravity="center"
            android:background="@drawable/tableborder"
            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:background="@drawable/tableborder"
            android:gravity="center"
            android:text=" $129.00" />

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

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

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

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

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

    </TableRow>

    <TableRow
        android:background="#F0F7F7"
        android:padding="5dp"/>

    <TableRow
        android:background="#F0F7F7"
        android:padding="6dp"/>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</TableLayout>

<Button
    android:id="@+id/btnguardarmercado"
    android:layout_width="562dp"
    android:layout_height="72dp"
    android:layout_marginStart="352dp"
    android:layout_marginTop="30dp"
    android:layout_marginEnd="352dp"
    android:background="@drawable/btn_gray"
    android:text="GUARDAR AVANCE"
    android:textSize="30sp"
    app:backgroundTint="@null"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tlMarcado" />

<Spinner
    android:id="@+id/spinner4"
    android:layout_width="105dp"
    android:layout_height="36dp"
    android:layout_marginStart="16dp"
    android:background="@drawable/green_border"
    app:layout_constraintStart_toEndOf="@+id/textView71"
    app:layout_constraintTop_toTopOf="@+id/spinner3" />

<ImageView
    android:id="@+id/imageView88"
    android:layout_width="33dp"
    android:layout_height="46dp"
    android:layout_marginStart="-34dp"
    app:layout_constraintBottom_toBottomOf="@+id/spinner4"
    app:layout_constraintStart_toEndOf="@+id/spinner4"
    app:layout_constraintTop_toTopOf="@+id/spinner4"
    app:srcCompat="@drawable/baseline_arrow_drop_down_24" />

<TextView
    android:id="@+id/textView71"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="7dp"
    android:text="Al"
    android:textColor="#040404"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="@+id/spinner3"
    app:layout_constraintStart_toEndOf="@+id/spinner3"
    app:layout_constraintTop_toBottomOf="@+id/textView73" />

</androidx.constraintlayout.widget.ConstraintLayout>

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.getParent() != null) {
            (tvCodigo.getParent() as ViewGroup).removeView(tvCodigo) // <- fix
        }
        a.addView(tvCodigo)

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

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

        tableLayout.addView(a)
    }while (cursor.moveToNext())    }

我不能让它工作:

ax6ht2ek

ax6ht2ek1#

1.代码的问题在于如何访问TableLayout中的TextViews。

这是TableLayout.xml的简化视图,其中显示了以下结构:

<TableLayout
  android:id="@+id/tlMarcado"
  .....>
  <TableRow
     .....>
     <TextView
       android:id="@+id/tvTarjetaML"
       ...../>
  </TableRow>
</TableLayout>

ID为(tvDescML、tvTarjetaML、tvPrecioML)的TextViews不是TableLayout的直接子项。它们实际上是TableLayout中TableRow的子级

2.代码试图通过TableLayout视图中的ID来获取TextView:

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

要解决此问题,代码需要首先找到TableRow,然后在该TableRow中找到TextView:

val a = TableRow(this)
a.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)

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

3.移除if条件以及后续的removeView()调用

此代码检查tvCodigo TextView是否有父视图并将其删除:

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

上面提供的上下文是将tvCodigo TextView添加到newTableRow,并将该TableRow添加到TableLayout。removeView()方法用于从其父视图层次结构中删除视图。
但是,由于代码正在创建一个新的TableRow(a),并且tvCodigo TextView被设置为子级,因此TextView不可能有现有的父级。这段代码是不必要的,应该删除,这不会影响代码的功能,并会使其更干净:

val a = TableRow(this)
a.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)

val tvCodigo = a.findViewById<TextView>(R.id.tvDescML)
tvCodigo.text = cursor.getString(0)
a.addView(tvCodigo)

4.提高代码性能

addView()方法被多次调用,频繁修改视图层次结构可能会影响性能。因此,只将该方法应用于do块末尾的TableLayout:

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

    val tvCodigo = a.findViewById<TextView>(R.id.tvDescML)
    tvCodigo.text = cursor.getString(0)

    val tvDescripcion = a.findViewById<TextView>(R.id.tvTarjetaML)
    tvDescripcion.text = cursor.getString(6)

    val tvPrecio = a.findViewById<TextView>(R.id.tvPrecioML)
    tvPrecio.text = cursor.getString(2)

    tableLayout.addView(a)
  } while (cursor.moveToNext())

这回答了这个问题,但是我会注意到@marcpetitvecino在这个问题上留下的评论。
使用RecyclerView是一种更好的方法,因为它非常灵活,内存效率高,滚动平滑,因为它可以回收/重用视图,并且非常模块化/可定制。

相关问题