如何解决运行程序时不显示 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>
编辑 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 举报,一经查实,本站将立刻删除。