如何在android的表中添加行

时间:2012-03-31 15:57:27

标签: java android eclipse

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
  <LinearLayout
    android:id="@+id/horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
      <ImageView
        android:id="@+id/myImView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:src="@drawable/image1" />
     <TextView
       android:id="@+id/textView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
    </LinearLayout>
    <ScrollView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" >
       <TableLayout
         android:id="@+id/myTable"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" >
       </TableLayout>
    </ScrollView>
  </LinearLayout>

我想在表格中添加n行&#34; myTable&#34;在运行时我该怎么办?

目前我正在使用此代码

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TableLayout myTable = (TableLayout) findViewById(R.id.myTable);

TextView tv = new TextView(this);
for (int x = 0; x < 5; x++) {
tv.setText("This is row number=" + (x + 1));
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
}

但它给了我这个错误

03-26 00:29:34.070:
    E/AndroidRuntime(2230): java.lang.RuntimeException: Unable to start activity ComponentInfo{assignment.1/assignment.1.myView}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

2 个答案:

答案 0 :(得分:2)

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
for (int x = 0; x < 5; x++) {
TextView tv = new TextView(this);
tv.setText("This is row number=" + (x + 1));
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
}

现在尝试一个小小的改变

答案 1 :(得分:0)

我想这是因为你每次在循环中创建行对象都是新的,但你不要用TextView对象做这个。

像这样它应该有效:

TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
TextView tv;
TableRow tr;
for (int x = 0; x < 5; x++) {
   tv = new TextView(this);
   tv.setText("This is row number=" + (x + 1));
   tr = new TableRow(this);
   tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
      LayoutParams.WRAP_CONTENT));
   tr.addView(tv);
   myTable.addView(tr, LayoutParams.FILL_PARENT,
      LayoutParams.WRAP_CONTENT);
}