运行程序时不显示 TableRow 和 textviews

问题描述

我创建了这个 TableLayout,然后是其中的 TableRow 和 3 个 TextView。 当我运行该应用程序时,它没有出现任何帮助?! 这是我的布局:

基本上我想在从我的 sqlite 数据库中检索数据时以编程方式添加更多 TableRows。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/TableLayoutv">

    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:gravity="center">

        <TextView
            android:text="Code"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="20sp"
            android:textStyle="bold">
        </TextView>
        <TextView
            android:text="Village"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20sp"
            android:textStyle="bold">
        </TextView>
        <TextView
            android:text="Pays"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20sp"
            android:textStyle="bold">
        </TextView>

    </TableRow>
</TableLayout>

enter image description here

编辑 2:

这是 MainActivity 类:

    import android.database.Cursor
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.widget.TableLayout
import android.widget.TableRow
import android.widget.TextView
import java.sql.sqlException

class ShowVillages : AppCompatActivity() {
    private val tablelayoutv: TableLayout = findViewById(R.id.TableLayoutv)
    private lateinit var db: DatabaseHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_show_villages)

        db = DatabaseHelper(this)
       buildTable()

    }

   private fun buildTable() {

       try {
           val crs: Cursor = db.getDataVillage2()
           if (crs.count != 0) {
               if (crs.movetoFirst()) {
                   do {
                       val rows = crs.count
                       val cols = crs.columnCount
                       // outer for loop
                       for (i in 1..rows) {
                           val row = TableRow(this)
                           row.layoutParams = TableRow.LayoutParams(
                               TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT
                           )
                           for (j in 1..cols) {
                               //creation text view
                               val tv1 = TextView(this)
                               tv1.layoutParams = TableRow.LayoutParams(
                                   TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT
                               )
                               tv1.gravity = Gravity.START
                               tv1.textSize = 10F
                               tv1.setPadding(0,5,5)
                               tv1.text = crs.getString(j)
                               row.addView(tv1)
                           }
                           tablelayoutv.addView(row)
                       }
                   } while (crs.movetoNext())
               }
           }
       } catch (msqlException: sqlException) {
           throw msqlException
       }

   }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)