java - Tablelayout应该将行数一一增加?

标签 java android web-services android-tablelayout

数据来自服务器,想要在tablelayout中显示数据,但是想要在服务器发出一个请求时显示一行...

最佳答案

您需要使用单表布局和单行 View ,可以根据您的数据重复。

xml:

<!-- THE DATA TABLE -->
        <TableLayout
                android:id="@+id/data_table"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >
                <TableRow>
                        <TextView
                                android:text="@string/th_id"
                                android:minWidth="50px"
                                />
                        <TextView
                                android:text="@string/th_text_one"
                                android:minWidth="125px"
                                />
                        <TextView
                                android:text="@string/th_text_two"
                                android:minWidth="125px"
                                />
                </TableRow>
        </TableLayout>

类别:

 // the table that displays the data
        TableLayout dataTable;
// THE DATA TABLE
        dataTable=(TableLayout)findViewById(R.id.data_table);


   /**
     * updates the table from the database.
     */
    private void updateTable()
    {


      // delete all but the first row.  remember that the count
      // starts at one and the index starts at zero


  while (dataTable.getChildCount() > 1)
    {
            // while there are at least two rows in the table widget, delete
            // the second row.
            dataTable.removeViewAt(1);
    }

        // collect the current row information from the Server and
        // store it in a two dimensional ArrayList
        ArrayList<ArrayList<Object>> data = getAllRowsAsArrays();

        // iterate the ArrayList, create new rows each time and add them
        // to the table widget.
        for (int position=0; position < data.size(); position++)
        {
                TableRow tableRow= new TableRow(this);

                ArrayList<Object> row = data.get(position);

                TextView idText = new TextView(this);
                idText.setText(row.get(0).toString());
                tableRow.addView(idText);

                TextView textOne = new TextView(this);
                textOne.setText(row.get(1).toString());
                tableRow.addView(textOne);

                TextView textTwo = new TextView(this);
                textTwo.setText(row.get(2).toString());
                tableRow.addView(textTwo);

                dataTable.addView(tableRow);
        }
    }

关于java - Tablelayout应该将行数一一增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17377356/

相关文章:

java - MPAndroid Chart 的 LineChart 创建错误,无法启动 Activity

java - Spring Boot无法运行data.sql来初始化数据库

android - 列出 Play 商店中发行商的所有产品

java - 如何获取 SOAP 请求客户端计算机的源 IP?

java - GWT - 如何在 @Entity 注释的类之外实现 RequestContext 方法?

java:如何从数据库获取值到jTable中

java - Android无法解析或不是字段

android - 向 SQLite 数据库 Android 添加另一列

java - 如何在异步线程中执行后台计算(在 REST Web 服务中)

visual-studio-2010 - 创建需要客户端证书的 Web 服务