61

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));
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
PiyushMishra
  • 5,743
  • 6
  • 37
  • 57

5 Answers5

100

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>
David Silva-Barrera
  • 1,006
  • 8
  • 12
sleep
  • 4,855
  • 5
  • 34
  • 51
  • I have 3 columns in my table, my doubt is how can I stretch my second column using this approach? – Victor Laerte Mar 27 '13 at 19:12
  • 2
    Very good. Easily portable to other types of Layout with other kinds of children. – igordc Apr 24 '13 at 21:01
  • i dont understand this part..ResourceBalance b : xmlDoc.balance_info – learner Sep 08 '15 at 07:11
  • @learner the "ResourceBalance b : xmlDoc.balance_info" can be removed. I'm assuming this is something specific to the code by jarrod as it did not resolve to any existing class when looking up. I removed it and without it my code worked just as expected. – Viral Patel Nov 30 '15 at 17:14
  • @JarrodSmith thank you for such perfect answer with good explanation. – Harin Kaklotar Nov 17 '17 at 07:31
27

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

blackpanther
  • 10,998
  • 11
  • 48
  • 78
Atmaram
  • 3,655
  • 2
  • 27
  • 33
3

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.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • actually the problem is whenever i am using a list view it need array of strings to show the data in the list but the data is updated every time when a button is clicked so its not an array of string. :( – PiyushMishra Mar 03 '11 at 17:42
  • you don't need an array of strings. When you use a CursorAdapter you are giving it a database cursor, which is exactly what you have. – Matthew Mar 03 '11 at 17:49
2

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.

David Silva-Barrera
  • 1,006
  • 8
  • 12
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
0

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.

Hong Ning
  • 1,003
  • 7
  • 7