android - 在 TableView 中显示结果集

标签 android mysql tablelayout

我真的需要帮助 我有一个 ResultSet 作为来自 MySQL DB 的选择查询的结果。如何在表格布局中显示它?我已经设置了表格 View 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:orientation="vertical">

<EditText
    android:id="@+id/myFilter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/some_hint" />
<TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:padding="10dp"
  android:text="@string/some_text" android:textSize="20sp" />
<TableLayout
 android:id="@+id/producttablelayout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:dividerPadding="@dimen/activity_horizontal_margin"
 android:scrollbars="horizontal|vertical"
 android:showDividers="none|beginning|middle|end" >

 <TableRow
     android:id="@+id/tableRow1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:gravity="top|bottom|left|right"
     android:paddingTop="@dimen/activity_horizontal_margin" >

     <TextView
         android:id="@+id/ttt1"
         android:text="Column 1"
         android:textAppearance="?android:attr/textAppearanceLarge" />

     <TextView
         android:id="@+id/ttt2"
         android:text="Column 12"
         android:textAppearance="?android:attr/textAppearanceLarge" />


 </TableRow>

 </TableLayout>

</LinearLayout>

我希望能够将 ResultSet 数据添加到其中。像这样的东西:

    super.onCreate(savedInstanceState);
     String s = null;
    setContentView(R.layout.activity_list_all_products);
    // Show the Up button in the action bar.
    setupActionBar();
    context333=this;
    TableLayout myTable = (TableLayout)findViewById(R.id.producttablelayout);
    try
    {
    connect2 = DriverManager.getConnection(LogonActivity.url, LogonActivity.user, LogonActivity.password);
    statement2 = connect2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
            ResultSet.CONCUR_READ_ONLY);
    preparedStatement2 = connect2.prepareStatement("select     article_code,article_desc from products limit 4");
    resultSet2=preparedStatement2.executeQuery();
   resultSet2.beforeFirst();

   while (resultSet2.next()) {
      how to add next record here to the table layout??

   }

请帮忙

最佳答案

您可以创建一个新的 View (看起来您正在使用 TextView 填充表行)并根据需要使用查询结果填充它们。然后,您可以使用构造函数 (see the documentation) 创建一个新的 TableRow

使用以下方法将 View 添加到表行实例:

tableRowInstance.addView(viewInstance);

然后以同样的方式将tableRow添加到tableLayout:

myTable.addView(tableRowInstance)

TableLayouts 和 TableRows 都是 View 的子类。您应该对 ViewGroup、View 以及它们的结构进行一些研究。 This link提供了很好的可视化效果。

关于android - 在 TableView 中显示结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659366/

相关文章:

html - 使用表格布局设置表格中的 td 宽度 : fixed

android - react native |向文本上下文菜单添加选项

android - 取消动画,停止 applyTransformation 运行

android - 在可扩展 ListView 中拖放(Android)

php - 如何搜索mysql DATE并返回结果以及日期和数据?

c# - MySQL 到数组到 MSSQL 查询语句

java - 库中的 generateReleaseBuildConfig 错误

mysql - 从结果中删除空值

ListView中的Android TableLayout,神秘的列收缩

Android - 在表格行内以编程方式设置按钮宽度