0

I have a LinearLayout that I programmatically update with a TableLayout using the code:

public void updateTableView(MyData data){
  DataTable dataTable = new DataTable(getApplicationContext(), data);
  LinearLayout placeHolder = (LinearLayout) findViewById(R.id.rel_view);
  placeHolder.addView(dataTable);
}

(For more background see Android RelativeLayout alignment based on View added programmatically.)

The first time the method updateTableView is called it works fine. But the LinearLayout seems to be ignoring all subsequent calls. I know this because the data is different each time but the app is only showing the result of the first call to updateTableView, i.e. the view is not changing.

Community
  • 1
  • 1
learner
  • 11,490
  • 26
  • 97
  • 169

1 Answers1

0

Probably because you don't remove previously added DataTable views. Try calling removeView():

private DataTable dataTable;

public void updateTableView(MyData data) {
  if (dataTable != null) {
      placeHolder.removeView(dataTable);
  }
  dataTable = new DataTable(getApplicationContext(), data);
  LinearLayout placeHolder = (LinearLayout) findViewById(R.id.rel_view);
  placeHolder.addView(dataTable);
}
Caner
  • 57,267
  • 35
  • 174
  • 180
  • 1
    Cool! It's working now. I just call `removeAllViews()` for my specific case. – learner Mar 15 '13 at 00:54
  • 1
    Great. Sorry I had a bug in my code, removeView should be called first(so that it removes the old one not the new one). I updated my code now. – Caner Mar 15 '13 at 01:00