我正在尝试创建一个表。然而,每次加载应用程序时,行数都会有所不同,因此我以编程方式添加它们。除了 mainLayout 之外,行还有自己的 xml 布局。但是,当尝试将行添加到主布局中的 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);
}
}
行.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 主线:
<?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)
最佳答案
首先。你在这里的目标是什么?您希望将 TableLayout 用作列表吗?如果是这样,您可以而且应该使用 ListView 来达到此目的。
The point of using ListView is to be able to scale to larger data sets by not having to create and layout views for all of the items up-front.
目前的问题是每次运行循环时,都会不断向 TableLayout 添加相同的对象
row =(TableRow) v.findViewById(R.id.tableRow);
...
tableLayout.addView(row);
并且由于它已经存在,因此它会抛出“指定的子级已经有父级”。您必须首先在子级的父级上调用removeView()'
您可以做的是拥有一个 ArrayAdapter,其中包含您创建的所有 TableRow View ,并像这样添加它们:
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 等。 您可以将 TableLayout 切换为 ListView(这是处理行中嵌套数据的正确元素)
You can find documentation for the ListView here 和 Check this example for a ListView with CustomAdapter
无论哪种方式,我都会很乐意为您提供帮助。 问候,
关于java - 由于未知父级而无法添加 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38689628/