Adding a definite number of rows in a table in java
<p>I have to create a table in java and the number of rows are not known prior to execution. So i have the following java code which creates the table. However , when I try to add the table row to the table layout it throws an illegal state exception. How would I solve this issue?</p>

<pre><code> Map&lt;Integer,TableRow&gt; kMap = null;
   for(int i=0;i&lt;noofrows;i++)
   {

         TableRow test=new TableRow(this);
          kMap = new HashMap&lt;Integer,TableRow&gt;();
         for(int k = 0; k &lt; 3; k++)
         {
           kMap.put(k, test);
         }

     }
TextView empty = new TextView(this);

// title column/row
TextView title = new TextView(this);
title.setText("Java Weather Table");


TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 6;

rowTitle.addView(title, params);

//Add some more fields to the rows

table1.addView(rowTitle);
table1.addView(kMap.get(0));
//Exception is thrown
table1.addView(kMap.get(1));
table1.addView(kMap.get(2));
//table1.addView(rowConditions);

setContentView(table1);
</code></pre>

<p>Log cat trace:</p>

<pre><code>09-14 07:02:50.265: E/AndroidRuntime(384): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addView(ViewGroup.java:1865)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.widget.TableLayout.addView(TableLayout.java:418)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addView(ViewGroup.java:1822)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.widget.TableLayout.addView(TableLayout.java:400)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addView(ViewGroup.java:1802)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.widget.TableLayout.addView(TableLayout.java:391)
</code></pre>
