title: Clearing a Table Layout dynamically
body: <p>I am currently working on an android app where I select a customer in an activity, it retrieves all records pertaining to that customer and displays them in a table layout in a new activity.
  The problem arises when I go back to the previous activity and select a different customer, the new records are added behind the records of the previous customer (meaning the previous table layout entries are not deleted when I press the back button and go to select a new customer).
  I've tried 2 solutions:</p>
  
  <pre><code>tl = (TableLayout) findViewById(R.id.maintable);
  tl.removeAllViewsInLayout();
  </code></pre>
  
  <p>and </p>
  
  <pre><code>layout = (RelativeLayout)findViewById(R.id.layout1);
  tl = (TableLayout) findViewById(R.id.maintable);
  int count=tl.getChildCount();
  for(int i=0;i&lt;count;i++)
  tl.removeView(layout.getChildAt(i));
  </code></pre>
  
  <p>Both these solutions do not work for me as the new data is still being added behind the existing data.
  Could anyone suggest me a different solution that could work?</p>
  
  <p>Also I am attaching the entire code.</p>
  
  <p>OutstandingBills.java:</p>
  
  <pre><code>@Override
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_outstanding_bills);
          layout = (RelativeLayout)findViewById(R.id.layout1);
          tl = (TableLayout) findViewById(R.id.maintable);
          tl.removeAllViewsInLayout();
          int count=tl.getChildCount();
          for(int i=0;i&lt;count;i++)
              tl.removeView(layout.getChildAt(i));
          addHeaders();
          addData();
      }
  
      public void addHeaders(){
  
          tr = new TableRow(OutstandingBills.this);
          tr.setLayoutParams(new LayoutParams(
                  LayoutParams.FILL_PARENT,
                  LayoutParams.WRAP_CONTENT));
  
          TextView bilDateTV = new TextView(OutstandingBills.this);
          bilDateTV.setText("Date");
          bilDateTV.setTextColor(Color.BLACK);
          bilDateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
          bilDateTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
          bilDateTV.setPadding(5, 5, 5, 0);
          tr.addView(bilDateTV);  // Adding textView to tablerow.
  
          TextView billNoTV = new TextView(OutstandingBills.this);
          billNoTV.setText("Bill No.");
          billNoTV.setTextColor(Color.BLACK);
          billNoTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
          billNoTV.setPadding(5, 5, 5, 0);
          billNoTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
          tr.addView(billNoTV); // Adding textView to tablerow.
  
          TextView dueAmtTV = new TextView(OutstandingBills.this);
          dueAmtTV.setText("Amount Due");
          dueAmtTV.setTextColor(Color.BLACK);
          dueAmtTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
          dueAmtTV.setPadding(5, 5, 5, 0);
          dueAmtTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
          tr.addView(dueAmtTV); // Adding textView to tablerow.
  
  
          tl.addView(tr, new TableLayout.LayoutParams(
                  LayoutParams.FILL_PARENT,
                  LayoutParams.WRAP_CONTENT));
  
          tr = new TableRow(OutstandingBills.this);
          tr.setLayoutParams(new LayoutParams(
                  LayoutParams.FILL_PARENT,
                  LayoutParams.WRAP_CONTENT));
  
          TextView divider = new TextView(OutstandingBills.this);
          divider.setText("-----------------");
          divider.setTextColor(Color.BLACK);
          divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
          divider.setPadding(5, 0, 0, 0);
          divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
          tr.addView(divider); // Adding textView to tablerow.
  
          TextView divider2 = new TextView(OutstandingBills.this);
          divider2.setText("-------------------------");
          divider2.setTextColor(Color.BLACK);
          divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
          divider2.setPadding(5, 0, 0, 0);
          divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
          tr.addView(divider2); // Adding textView to tablerow.
  
          TextView divider3 = new TextView(OutstandingBills.this);
          divider3.setText("-------------------------");
          divider3.setTextColor(Color.BLACK);
          divider3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
          divider3.setPadding(5, 0, 0, 0);
          divider3.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
          tr.addView(divider3); // Adding textView to tablerow.
  
          // Add the TableRow to the TableLayout
          tl.addView(tr, new TableLayout.LayoutParams(
                  LayoutParams.FILL_PARENT,
                  LayoutParams.WRAP_CONTENT));
      }
  
      public void addData(){
  
          for (int i = 0; i &lt; arr.size(); i++)
          {
              tr = new TableRow(OutstandingBills.this);
              tr.setLayoutParams(new LayoutParams(
                      LayoutParams.FILL_PARENT,
                      LayoutParams.WRAP_CONTENT));
  
              bilDate = new TextView(OutstandingBills.this);
              bilDate.setText(arr.get(i).toString());
              bilDate.setTextColor(Color.BLACK);
              bilDate.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
              bilDate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
              bilDate.setPadding(5, 5, 5, 5);
              tr.addView(bilDate);  // Adding textView to tablerow.
  
              bilNo = new TextView(OutstandingBills.this);
              bilNo.setText(arr1.get(i).toString());
              bilNo.setTextColor(Color.BLACK);
              bilNo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
              bilNo.setPadding(5, 5, 5, 5);
              bilNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
              tr.addView(bilNo); // Adding textView to tablerow.
  
              dueAmt = new TextView(OutstandingBills.this);
              dueAmt.setText(arr2.get(i).toString());
              dueAmt.setTextColor(Color.BLACK);
              dueAmt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
              dueAmt.setPadding(5, 5, 5, 5);
              dueAmt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
              tr.addView(dueAmt); // Adding textView to tablerow.
  
              // Add the TableRow to the TableLayout
              tl.addView(tr, new TableLayout.LayoutParams(
                      LayoutParams.FILL_PARENT,
                      LayoutParams.WRAP_CONTENT));
          }
      }
  </code></pre>
  
  <p>OutstandingBills.xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:id="@+id/layout1"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context="com.example.ashwin.projectx1.OutstandingBills"&gt;
  
      &lt;TextView android:text="@string/hello_world" android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
      &lt;ScrollView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:scrollbars="none"&gt;
          &lt;TableLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:stretchColumns="0,1"
              android:id="@+id/maintable" &gt;
          &lt;/TableLayout&gt;
      &lt;/ScrollView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Implementing @ Mr.Neo 's solution:</p>
  
  <p>This is how I implemented your solution in my code:</p>
  
  <pre><code>        addHeaders();
  
          new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
                  //Add view for table layout here
  
                  addData();
              }
          }, 2000);
  </code></pre>
  
  <p>But there is still no effect on the TableLayout</p>
  
  <p>Screenshots:
  <a href="http://i.stack.imgur.com/U3lyI.png" rel="nofollow">The first time I enter the activity with a Customer name</a></p>
  
  <p><a href="http://i.stack.imgur.com/JCHuJ.png" rel="nofollow">On re-entering the activity the second time, you can see the duplicate records</a></p>