0

我已经编写了这段代码来将 textview 添加到表格行中,但是我对行数有问题,我所做的表格行数是恒定的(5 行),我想要的是表格行数是动态的。所以我想循环这段代码,但我不知道怎么做。你能教我怎么做吗?谢谢你 :)

这是代码:

   TableRow row0 = new TableRow(this);
    TableRow row1 = new TableRow(this);
    TableRow row2 = new TableRow(this);
    TableRow row3 = new TableRow(this);
    TableRow row4 = new TableRow(this);

    row0.addView(createTextView(listTipe.get(0).getType()));
    row1.addView(createTextView(listTipe.get(1).getType()));
    row2.addView(createTextView(listTipe.get(2).getType()));
    row3.addView(createTextView(listTipe.get(3).getType()));
    row4.addView(createTextView(listTipe.get(4).getType()));

    row0.addView(createTextView(listTotal1.get(0).getTotal()));
    row1.addView(createTextView(listTotal1.get(1).getTotal()));
    row2.addView(createTextView(listTotal1.get(2).getTotal()));
    row3.addView(createTextView(listTotal1.get(3).getTotal()));
    row4.addView(createTextView(listTotal1.get(4).getTotal()));

    row0.addView(createTextView(listTotal2.get(0).getTotal()));
    row1.addView(createTextView(listTotal2.get(1).getTotal()));
    row2.addView(createTextView(listTotal2.get(2).getTotal()));
    row3.addView(createTextView(listTotal2.get(3).getTotal()));
    row4.addView(createTextView(listTotal2.get(4).getTotal()));

    row0.addView(createTextView(listTotal3.get(0).getTotal()));
    row1.addView(createTextView(listTotal3.get(1).getTotal()));
    row2.addView(createTextView(listTotal3.get(2).getTotal()));
    row3.addView(createTextView(listTotal3.get(3).getTotal()));
    row4.addView(createTextView(listTotal3.get(4).getTotal()));

    row0.addView(createTextView(listUang1.get(0).getUang()));
    row1.addView(createTextView(listUang1.get(1).getUang()));
    row2.addView(createTextView(listUang1.get(2).getUang()));
    row3.addView(createTextView(listUang1.get(3).getUang()));
    row4.addView(createTextView(listUang1.get(4).getUang()));

    row0.addView(createTextView(listUang2.get(0).getUang()));
    row1.addView(createTextView(listUang2.get(1).getUang()));
    row2.addView(createTextView(listUang2.get(2).getUang()));
    row3.addView(createTextView(listUang2.get(3).getUang()));
    row4.addView(createTextView(listUang2.get(4).getUang()));

    row0.addView(createTextView(listUang3.get(0).getUang()));
    row1.addView(createTextView(listUang3.get(1).getUang()));
    row2.addView(createTextView(listUang3.get(2).getUang()));
    row3.addView(createTextView(listUang3.get(3).getUang()));
    row4.addView(createTextView(listUang3.get(4).getUang()));

    tableLayout.addView(row0);
    tableLayout.addView(row1);
    tableLayout.addView(row2);
    tableLayout.addView(row3);
    tableLayout.addView(row4);

  }

  private TextView createTextView(String text) {
    TextView textView = new TextView(this);
    textView.setText(text);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setPadding(0, 0, 15, 0);
    return textView;

  }
4

1 回答 1

2
for (int i = 0; i < 5; i++) {
  TableRow tableRow = new TableRow(this);
  tableRow.addView(createTextView(listTipe.get(i).getType());
  tableRow.addView(createTextView(listTotal1.get(i).getTotal()));
  tableRow.addView(createTextView(listTotal2.get(i).getTotal()));
  tableRow.addView(createTextView(listTotal3.get(i).getTotal()));
  tableRow.addView(createTextView(listTotal1.get(i).getUang()));
  tableLayout.addView(tableRow);
}
于 2012-06-29T04:16:47.037 回答