我已经编写了这段代码以将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;

  }

最佳答案

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);
}

关于java - 如何循环为表行添加 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11255946/

10-11 04:54