Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add rows dynamically into table layout

Tags:

android

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 shows 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)); 
like image 564
PiyushMishra Avatar asked Mar 03 '11 17:03

PiyushMishra


People also ask

How to add table rows Dynamically in Android studio?

This example demonstrates how to add table rows Dynamically in Android Layout. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you use table layout?

Android TableLayout going to be arranged groups of views into rows and columns. You will use the <TableRow> element to build a row in the table. Each row has zero or more cells; each cell can hold one View object.

What is table row in Android?

Tables are uniquely identified by their names and are comprised of columns and rows. Columns contain the column name, data type, and any other attributes for the column. Rows contain the records or data for the columns.


2 Answers

Here's technique I figured out after a bit of trial and error that allows you to preserve your XML styles and avoid the issues of using a <merge/> (i.e. inflate() requires a merge to attach to root, and returns the root node). No runtime new TableRow()s or new TextView()s required.

Code

Note: Here CheckBalanceActivity is some sample Activity class

TableLayout table = (TableLayout)CheckBalanceActivity.this.findViewById(R.id.attrib_table); for(ResourceBalance b : xmlDoc.balance_info) {     // Inflate your row "template" and fill out the fields.     TableRow row = (TableRow)LayoutInflater.from(CheckBalanceActivity.this).inflate(R.layout.attrib_row, null);     ((TextView)row.findViewById(R.id.attrib_name)).setText(b.NAME);     ((TextView)row.findViewById(R.id.attrib_value)).setText(b.VALUE);     table.addView(row); } table.requestLayout();     // Not sure if this is needed. 

attrib_row.xml

<?xml version="1.0" encoding="utf-8"?> <TableRow style="@style/PlanAttribute"  xmlns:android="http://schemas.android.com/apk/res/android">     <TextView         style="@style/PlanAttributeText"         android:id="@+id/attrib_name"         android:textStyle="bold"/>     <TextView         style="@style/PlanAttributeText"         android:id="@+id/attrib_value"         android:gravity="right"         android:textStyle="normal"/> </TableRow> 
like image 105
Jarrod Smith Avatar answered Sep 30 '22 07:09

Jarrod Smith


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...

like image 22
Atmaram Avatar answered Sep 30 '22 07:09

Atmaram