如何循环表行的addview?

时间:2012-06-29 04:09:12

标签: java android loops

我已编写此代码以将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;

  }

1 个答案:

答案 0 :(得分: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);
}