How to add rows to table in Android

2

Good morning guys, I am new to Android, I have consulted many sites and would like to know how to add rows or records to a table on Android, I appreciate the collaboration.

Layout Code:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    <TableRow
        android:id="@+id/Cabecera"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/ColumnaCedula"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="Cédula"
            android:textColor="#005500"
            android:textSize="21dp" />

        <TextView
            android:id="@+id/ColumnaNombre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="5px"
            android:text="Nombre Operario"
            android:textColor="#005500"
            android:textSize="21dp" />

        <TextView
            android:id="@+id/ColumnaCargo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="Cargo"
            android:textColor="#005500"
            android:textSize="21dp" />
    </TableRow>

    <TableRow
        android:id="@+id/SeparadorCabecera"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@+id/LineaCabecera"
            android:layout_width="fill_parent"
            android:layout_height="2px"
            android:layout_span="6"
            android:background="#FFFFFF" >
        </FrameLayout>
    </TableRow>

</TableLayout>

Java Code: Add a row to the table when the button is clicked

public void AgregarFilaTabla(View view)
{
   TableLayout tabla = (TableLayout) findViewById(R.id.tblOperarios);

   String params[] = {"Dato 1", "Dato 2", "Dato 2"};
   TableRow row = new TableRow(params);
   tabla.addView(row);
}
    
asked by Felipe Mendieta Perez 09.02.2017 в 13:52
source

1 answer

1

To instantiate the TableRow you must use the context, and not an array:

TableRow row = new TableRow(this);

and for the text you can add a TextView

TextView tv = new TextView(this);
tv.setText("Dato 1");
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

and then add the element to TableRow :

row.addView(tv);

the same to add TableRow to TableLayout :

TableRow.addView(row);
    
answered by 09.02.2017 / 17:17
source