在Android中动态添加表格行 [英] Adding Table rows Dynamically in Android
问题描述
我正在尝试创建一个需要动态添加表格行的布局.下面是表格布局xml
I am trying to create a layout where I need to add table rows dynamically. Below is the table layout xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/displayLinear"
android:background="@color/background_df"
android:orientation="vertical" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/display_row"
android:layout_marginTop="280dip" >
</TableLayout>
动态添加行的活动文件是
The activity file where rows are being added dynamically is
public void init(){
menuDB = new MenuDBAdapter(this);
ll = (TableLayout) findViewById(R.id.displayLinear);
TableRow row=(TableRow)findViewById(R.id.display_row);
for (int i = 0; i <2; i++) {
checkBox = new CheckBox(this);
tv = new TextView(this);
addBtn = new ImageButton(this);
addBtn.setImageResource(R.drawable.add);
minusBtn = new ImageButton(this);
minusBtn.setImageResource(R.drawable.minus);
qty = new TextView(this);
checkBox.setText("hello");
qty.setText("10");
row.addView(checkBox);
row.addView(minusBtn);
row.addView(qty);
row.addView(addBtn);
ll.addView(row,i);
}
}
但是当我运行这个时,我得到以下错误
But when I run this, I am getting below error
08-13 16:27:46.437: E/AndroidRuntime(23568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.roms/com.example.roms.DisplayActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我知道这是由于命令 ll.addView(row,i);
但是当我删除它时,它会将所有内容添加到一行中,而不是为下一项创建新行.我也尝试将索引作为 row.addView(addBtn,i)
但仍然没有正确填充.请指教.谢谢.
I understand that this is due to command ll.addView(row,i);
but when I remove this its adding all stuff in a single row rather tan creating a new row for next item. I tried with giving index too as row.addView(addBtn,i)
but still its not populating correctly. Please advise. Thanks.
推荐答案
您不应该使用在布局 XML 中定义的项目来创建它的更多实例.您应该在单独的 XML 中创建它并对其进行扩充,或者以编程方式创建 TableRow.如果以编程方式创建它们,应该是这样的:
You shouldn't be using an item defined in the Layout XML in order to create more instances of it. You should either create it in a separate XML and inflate it or create the TableRow programmaticaly. If creating them programmaticaly, should be something like this:
public void init(){
TableLayout ll = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
checkBox = new CheckBox(this);
tv = new TextView(this);
addBtn = new ImageButton(this);
addBtn.setImageResource(R.drawable.add);
minusBtn = new ImageButton(this);
minusBtn.setImageResource(R.drawable.minus);
qty = new TextView(this);
checkBox.setText("hello");
qty.setText("10");
row.addView(checkBox);
row.addView(minusBtn);
row.addView(qty);
row.addView(addBtn);
ll.addView(row,i);
}
}
这篇关于在Android中动态添加表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!