How to add rows dynamically into table layout

Android

Android Problem Overview


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));

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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

Solution 3 - Android

You might be better off using a ListView with a CursorAdapter (or SimpleCursorAdapter).

These are built to show rows from a sqlite database and allow refreshing with minimal programming on your part.

Edit - here is a tutorial involving SimpleCursorAdapter and ListView including sample code.

Solution 4 - Android

You are doing it right; every time you need to add a row, simply so new TableRow(), etc. It might be easier for you to inflate the new row from XML though.

Solution 5 - Android

Solution 1. Set your TableLayout tl as class member variable, only call TableLayout tl=(TableLayout)findViewById(R.id.maintable); when you initiate the class. When button clicked use tl directly, eg. tl.addView(row), don't call FindViewById anymore. So the next new row wouldn't replace the previous new row.

Solution 2. Everytime after button click save your updated data into an array, and then re-render your whole table layout by loop through the array.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPiyushMishraView Question on Stackoverflow
Solution 1 - AndroidJarrod SmithView Answer on Stackoverflow
Solution 2 - AndroidAtmaramView Answer on Stackoverflow
Solution 3 - AndroidMatthew WillisView Answer on Stackoverflow
Solution 4 - AndroidRomain GuyView Answer on Stackoverflow
Solution 5 - AndroidHong NingView Answer on Stackoverflow