android-使用文本条目中的数据动态地将行添加到表中
我试图从文本条目中捕获数据,并将数据添加到表布局中,然后刷新布局。当我单击“添加”按钮时,会弹出一个对话框,其中包含文本字段。输入任何内容并单击“确定”后,它将创建一个新行并刷新视图 现在,我不是从文本条目中捕获数据。我只是尝试使用伪数据向表布局添加伪数据,但我一直得到“指定的子级已经有父级。必须首先对子级的父级调用removeView()”。在这一行 table.addView(tr,newtablelayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容)) 我是android开发的初学者。如果有人能给我一些建议,那就太好了 谢谢。你可以看看 希望有帮助 干杯你可以看看 希望有帮助android-使用文本条目中的数据动态地将行添加到表中,android,tablelayout,Android,Tablelayout,我试图从文本条目中捕获数据,并将数据添加到表布局中,然后刷新布局。当我单击“添加”按钮时,会弹出一个对话框,其中包含文本字段。输入任何内容并单击“确定”后,它将创建一个新行并刷新视图 现在,我不是从文本条目中捕获数据。我只是尝试使用伪数据向表布局添加伪数据,但我一直得到“指定的子级已经有父级。必须首先对子级的父级调用removeView()”。在这一行 table.addView(tr,newtablelayout.LayoutParams(LayoutParams.FILL\u父级,Lay
干杯我假设
tr
是一个您正在重用的变量。您需要为要添加的每一行创建一个新的TableRow
如果希望用XML定义表和行,可以使用以下方法
首先,创建两个布局文件,一个用于表格,另一个用于表格行,例如:
table_header.xml
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" style="@style/TableHeader">
<TextView style="@style/TableRowHeaderCell" android:text="@string/index" />
<TextView style="@style/TableRowHeaderCell" android:text="@string/count" />
</TableRow>
要添加新行,可以使用以下命令:
// create the row with the xml layout
final View row = inflater.inflate(R.layout.table_row, null);
// set values in row
((TextView)row.findViewById(R.id.textViewIndex)).setText("someText");
// finally add the row
_tableLayoutResults.addView(row);
我假设
tr
是一个您正在重用的变量。您需要为要添加的每一行创建一个新的TableRow
如果希望用XML定义表和行,可以使用以下方法
首先,创建两个布局文件,一个用于表格,另一个用于表格行,例如:
table_header.xml
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" style="@style/TableHeader">
<TextView style="@style/TableRowHeaderCell" android:text="@string/index" />
<TextView style="@style/TableRowHeaderCell" android:text="@string/count" />
</TableRow>
要添加新行,可以使用以下命令:
// create the row with the xml layout
final View row = inflater.inflate(R.layout.table_row, null);
// set values in row
((TextView)row.findViewById(R.id.textViewIndex)).setText("someText");
// finally add the row
_tableLayoutResults.addView(row);
请为您的更新方法尝试以下代码:
private void update() {
TableLayout table = (TableLayout)findViewById(R.id.table);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("New Entry");
tr.addView(tv);
table.addView(tr);
}
重要的是:
- 将
添加到TableRow
时,请使用TableLayout
TableLayout.LayoutParams
- 将
添加到视图
时,请使用表格行
表格行.布局参数
private void update() {
TableLayout table = (TableLayout)findViewById(R.id.table);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("New Entry");
tr.addView(tv);
table.addView(tr);
}
重要的是:
- 将
添加到TableRow
时,请使用TableLayout
TableLayout.LayoutParams
- 将
添加到视图
时,请使用表格行
表格行.布局参数
private void update() {
TableLayout table = (TableLayout)findViewById(R.id.table);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("New Entry");
tr.addView(tv);
table.addView(tr);
}