Skip to content

Instantly share code, notes, and snippets.

@ajpinedam
Created July 14, 2014 14:57
Show Gist options
  • Save ajpinedam/c53af0e335f6a2762516 to your computer and use it in GitHub Desktop.
Save ajpinedam/c53af0e335f6a2762516 to your computer and use it in GitHub Desktop.
Create TableLayout Android
public void insertRealDataToTable(List<Person> people)
{
float scale = getResources().getDisplayMetrics().density;
int leftPad = (int) (getResources().getDimension(R.dimen.tbl_row_left_pad)*scale + 0.5f);
int topPad = (int) (getResources().getDimension(R.dimen.tbl_row_top_pad)*scale + 0.5f);
int rightPad = (int) (getResources().getDimension(R.dimen.tbl_row_right_pad)*scale + 0.5f);
int bottomPad = (int) (getResources().getDimension(R.dimen.tbl_row_bottom_pad)*scale + 0.5f);
if (people != null){
refreshDataTable();
int i=0;
for(Person _per : people){
i++;
_row = new TableRow(getApplicationContext());
TextView tvNumber = new TextView(getApplicationContext());
TextView tvId = new TextView(getApplicationContext());
TextView tvName = new TextView(getApplicationContext());
TextView tvLastName = new TextView(getApplicationContext());
tvNumber.setPadding(leftPad,
topPad,
rightPad,
bottomPad);
tvId.setPadding(leftPad,
topPad,
rightPad,
bottomPad);
tvName.setPadding(leftPad,
topPad,
rightPad,
bottomPad);
tvLastName.setPadding(leftPad,
topPad,
rightPad,
bottomPad);
tvNumber.setText( String.valueOf(i));
tvId.setText( String.valueOf(_per.Id()));
tvName.setText( _per.Name() );
tvLastName.setText( _per.LastName());
_row.addView(tvNumber);
_row.addView(tvId);
_row.addView(tvName);
_row.addView(tvLastName);
// _personTable is a TableLayout object defined at the Layout and initialized at the OnCreate
_personTable.addView(_row);
// Change Color of even rows
if (((i) % 2) == 0) {
_row.setBackgroundColor(Color.parseColor("#C3D3D3"));
}
}
}
}
/// Clear all rows but the Header when new records come up.
private void refreshDataTable(){
_personTable.removeViews(1, _personTable.getChildCount()-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment