在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.addView(addBtn,我)
,但仍是其不填充正确。请指教。谢谢你。
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创建它,它充气或programmaticaly创建的TableRow。如果programmaticaly创建它们,应该是这样的:
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屋!