Extending TableLayout class - adding rows
I am trying to extend the TableLayout class, such开发者_StackOverflow that rows will be populated automatically by the class. The issue I am having is the rows that I add inside the custom class do not display.
Ex. within the Activity:
private TextView getTableCell(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatasetTableLayout table = (DatasetTableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(this);
tr.addView(getTableCell("activity1"));
tr.addView(getTableCell("activity2"));
tr.addView(getTableCell("activity3"));
table.addView(tr);
This successfully adds a row to the table. However, within my custom class:
private TextView getTableCell(String text) {
TextView tv = new TextView(context);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
private void update() {
TableRow tr = new TableRow(context);
tr.addView(getTableCell("class1"));
tr.addView(getTableCell("class2"));
tr.addView(getTableCell("class3"));
addView(tr);
}
does not successfully add a row. Well, it does add a row, as getChildCount(), and getChildAt() do return this row - but it does not get displayed.
Am I missing something?
@theresia (Can't seem to format text if I add a comment): It does appear that it adds the TableRow:
for(int i = 0; i < getChildCount(); i++) {
TableRow row = (TableRow) getChildAt(i);
for(int j = 0; j < row.getChildCount(); j++) {
TextView tv = (TextView) row.getChildAt(j);
Log.e("DatasetTableLayout-data", tv.getText().toString() + " ");
}
}
gives me:
01-24 11:03:31.668: ERROR/DatasetTableLayout-data(1624): activity1
01-24 11:03:31.677: ERROR/DatasetTableLayout-data(1624): activity2
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): activity3
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class1
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class2
01-24 11:03:31.727: ERROR/DatasetTableLayout-data(1624): class3
My guess would be something along this line: addView(tr);
Does it successfully add your TableRow
to TableLayout
?
Edit:
TableLayout tb = new TableLayout(this);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("row");
tr.addView(tv);
tb.addView(tr); // this is what I mean
setContentView(tb);
Your code for creating rows works fine, but if you haven't add it to the table, and later on, add the table to the parent view as well, your rows wouldn't get displayed.
精彩评论