Only show one row(last data)如何添加多个表格行?
我没有得到多行。只显示一行。如何解决这个问题呢。
//活动
public class TableViewActivity extends AppCompatActivity {
TableLayout tableLayout;
TableRow tableRow;
TextView textView1, textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_view);
tableLayout = (TableLayout) findViewById(R.id.tableLayout);
tableRow = (TableRow) findViewById(R.id.tableRow2);
textView1 = (TextView) findViewById(R.id.TextViewRowTwo1);
textView2 = (TextView) findViewById(R.id.TextViewRowTwo2);
List<Student> students = new ArrayList<>();
students.add(new Student(1, "Mahmud"));
students.add(new Student(2, "Saiful"));
students.add(new Student(3, "Monir"));
students.add(new Student(4, "Milton"));
for (int j = 0; j< students.size(); j++) {
textView1.setText(students.get(j).getId()+"");
textView2.setText(students.get(j).getName());
// TEXTVIEW
if(textView1.getParent()!=null)
((ViewGroup)textView1.getParent()).removeView(textView1); // <- fix
tableRow.addView(textView1);
if(textView2.getParent()!=null)
((ViewGroup)textView2.getParent()).removeView(textView2); // <- fix
tableRow.addView(textView2);
if(tableRow.getParent()!=null)
((ViewGroup)tableRow.getParent()).removeView(tableRow); // <- fix
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}
}
谢谢,但它不能工作给我。运行这些代码后,我的应用程序崩溃了。 –
请粘贴日志。 –
java.lang.IllegalStateException:指定的子项已经有父项。您必须先调用子对象的父对象的removeView()。谢谢 –