PiyushMishra PiyushMishra - 4 months ago 45
Android Question

How to add rows dynamically into table layout

I have certain data in sqllite and it update every time whenever, I click on save button and i want to show the data into a table layout for adding more rows for updated data.I have certain code but it show only the updated data replacing previous data and i want to add more rows as the data updated. I know this is only to add one row into table layout but how can i add more rows.

TableLayout tl=(TableLayout)findViewById(R.id.maintable);
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
textview.setText(data);
//textview.getTextColors(R.color.)
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
tl.addView(tr1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

Answer

The way you have added a row into the table layout you can add multiple TableRow instances into your tableLayout object

tl.addView(row1);
tl.addView(row2);

etc...