我正在尝试创建一个表。然而,每次加载应用程序时行数都会有所不同,所以我以编程方式添加它们。除mainLayout外,行还有自己的xml布局。但是,当尝试在mainlayout中向tableview添加行时,我得到一个错误(tableLayou.addView(row))。这是我的代码:
private void makeTable() {
View v = getLayoutInflater().inflate(R.layout.row, null);
TableRow row;
AutofitTextView textView;
Configuration configuration = getResources().getConfiguration();
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 1, getResources().getDisplayMetrics());
int width = (configuration.screenWidthDp/8)*dip;
int height = (configuration.screenHeightDp/10)*dip;
v.setBackgroundColor(getResources().getColor(R.color.black));
for (int i = 0; i<rowss.size();i++) {
row =(TableRow) v.findViewById(R.id.tableRow);
Scanner scanner = new Scanner(rowss.get(i));
while(scanner.hasNextLine()) {
Scanner s = new Scanner(scanner.nextLine());
AutofitTextView textView1 = (AutofitTextView) row.findViewById(R.id.manufacturer);
textView1.setText(s.next());
}
tableLayout.addView(row);
}
}
Row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableRow"
android:weightSum="8">
<me.grantland.widget.AutofitTextView
android:id="@+id/manufacturer"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:id="@+id/name"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:id="@+id/reason"
android:layout_weight="1"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/block"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/carrier"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/quantity"
android:layout_height="wrap_content" />
<me.grantland.widget.AutofitTextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/inventory"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="0dp"
android:id="@+id/status"
android:layout_weight="1"
android:layout_height="wrap_content" />
</TableRow>
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_main">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"/>
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
错误:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4654)
at android.view.ViewGroup.addView(ViewGroup.java:4490)
at android.widget.TableLayout.addView(TableLayout.java:426)
at android.view.ViewGroup.addView(ViewGroup.java:4431)
at android.widget.TableLayout.addView(TableLayout.java:408)
at android.view.ViewGroup.addView(ViewGroup.java:4404)
at android.widget.TableLayout.addView(TableLayout.java:399)
at com.intellidev.earlyupgradepicklist.MainActivity.makeTable(MainActivity.java:460)
at com.intellidev.earlyupgradepicklist.MainActivity.access$300(MainActivity.java:64)
at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:411)
at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:331)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
首先。你的目标是什么?您希望将TableLayout用作List吗?如果是这样,您可以而且应该使用ListView来实现此目的。
使用ListView的目的是能够扩展到更大的数据集 无需为所有项目创建和布局视图 的前期。
目前的问题是每次运行循环时,都会继续向TableLayout添加相同的对象
row =(TableRow) v.findViewById(R.id.tableRow);
...
tableLayout.addView(row);
并且由于它已经存在,它会抛出指定的子节点已经有父节点。您必须首先在孩子的父母上调用removeView()&#39;
你可以做的是有一个ArrayAdapter,它包含你创建的所有TableRow视图,并像这样添加它们:
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
tableLayout.addView(createTableRow(adapter.getItem(i)); // or tableLayout.addView(adapter.getView(i, null, tableLayout));
}
总的来说,你的table_row.xml看起来像是一个列表的入口,你有一个制造商,一个id等。 您可以为ListView切换TableLayout(这是处理行中嵌套数据的正确元素)
You can find documentation for the ListView here 和 Check this example for a ListView with CustomAdapter
无论哪种方式,我都很乐意帮助你。 的问候,