Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to dynamically create data tables in android
How to dynamically create data tables in android
Add a table to the layout

& lt table layout

Android:layout _ width = " match _ parent "

Android:layout _ height = " match _ parent "

Android:id = " @+id/table 1 " & gt;

& lt/table layout & gt;

Then write the dynamically added code in MainActivity.

Public void click (view v) {

if(row.getText()。 length()>; 0 & amp& ampcolumn.getText()。 length()>; 0){

//Convert input rows and columns into shapes.

int row _ int = integer . parse int(row . gettext()。 toString());

int col _ int = integer . parse int(column . gettext()。 toString());

//Get the layout of the control table

tableLayout =(tableLayout)findViewById(r . id . table 1);

//Clear all rows in the table.

table layout . remove all views();

//All columns are automatically filled with spaces.

table layout . setstretchallcolumns(true);

//Generate a table with x rows and y columns.

for(int I = 1; I<= row _ inti++)

{

TableRow TableRow = new TableRow(main activity . this);

for(int j = 1; j & lt= col _ intj++)

{

//TV is used for display.

TextView TV = new TextView(main activity . this);

//Button Bt = new Button(main activity . this);

TV . settext("+I+","+j+");

TableRow.addView (TV);

}

//Add the newly created TableRow to TableLayout.

tableLayout.addView(tableRow,new TableLayout。 LayoutParams(MP,WC, 1));

}

} Otherwise {

Toast. maketext (mainactivity。 Well, "Please enter a numerical value", 1). show();

}

}