title: android - Dynamically add row to a table with data from the text entry
body: <p>I am trying to capture data from the text entry and add the data to the table layout and refresh the layout. When I click "Add" button, a dialog pops up with the text field. After I enter anything and click "OK", it should create a new row and refresh the view.</p>
  
  <p>
  </p>
  
  <pre><code>&lt;TableLayout android:id="@+id/table"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:background="#f7ad5d"
               android:weightSum="1"&gt;
  
      &lt;TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tableRow1"&gt;
          &lt;Button android:layout_width="100px" android:text="Add" android:textSize="13px" android:layout_height="50px" android:id="@+id/add_button"&gt;&lt;/Button&gt;
      &lt;/TableRow&gt;
      &lt;TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tableRow2"&gt;
          &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:text="  My List"&gt;&lt;/TextView&gt;
      &lt;/TableRow&gt;
  
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>    @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      Button addBtn = (Button) findViewById(R.id.add_button);
      addBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              showDialog();
          }
      });
  }
  
      private void update() {
      TableLayout table = (TableLayout)findViewById(R.id.table);
      TableRow tr = new TableRow(this);
      tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  
      TextView tv = new TextView(this);
      tv.setText("New Entry");
      tr.addView(tv);
  
      table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  }
  
  
      /**
   * Show a dialog
   */
  private void showDialog() {
      final AlertDialog.Builder alert = new AlertDialog.Builder(this);
      final EditText input = new EditText(this);
      alert.setView(input);
      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
              String value = input.getText().toString().trim();
              Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
              finish();
              startActivity(getIntent());
              update();
          }
      });
  
      alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
              dialog.cancel();
          }
      });
      alert.show();
  }
  </code></pre>
  
  <p>For now, I am not capturing data from the text entry. I am just trying to add a dummy data to the table layout with dummy data, but i keep getting "The specified child already has a parent. You must call removeView() on the child's parent first." at this line,</p>
  
  <p>table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));</p>
  
  <p>I am a beginner to android development. It would be great if someone can give me some tips about this.</p>
  
  <p>Thanks.</p>