Я хочу, как только я нажму на кнопку, появится фрагмент, содержащий таблицу.
Отображается все, кроме макета таблицы.
Я не понял, почему.
Вот мой макет стола:
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:scrollingCache="true">
<TableLayout android:id="@+id/simpleTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="18dp" android:text="Row 1" android:layout_span="3"
android:padding="18dip" android:background="#b0b0b0"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="18dp" android:text="Row 1" android:layout_span="3"
android:padding="18dip" android:background="#b0b0b0"
android:textColor="#000"/>
</TableRow>
</TableLayout>
</android.support.v4.widget.NestedScrollView>
и вот важные методы моего фрагмента:
class TablePresentation : DialogFragment(),tableContract.View
{
...
override fun onCreateView(inflater: LayoutInflater?, parent: ViewGroup?, state: Bundle?): View? {
super.onCreateView(inflater, parent, state)
val view = activity.layoutInflater.inflate(R.layout.table_fragment, parent, false)
view.findViewById<View>(R.id.button_close)?.setOnClickListener({ dismiss() })
return view
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
table = view?.findViewById<View>(R.id.simpleTableLayout) as TableLayout
row = LayoutInflater.from(context).inflate(R.layout.attrib_row, null) as TableRow
attName= (row?.findViewById<View>(R.id.attrib_name) as TextView)
attVal=(row?.findViewById<View>(R.id.attrib_value) as TextView)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
}
...
}
После принятия первого ответа появляются таблицы, но как только я пытаюсь сделать это динамически, используя этот код:
class TablePresentation : DialogFragment(),tableContract.View
{
...
var table: TableLayout?=null
var row:TableRow?=null
var attName:TextView?=null
var attVal:TextView?=null
...
override fun updateTable(temps: ArrayList<Double>) {
for (i in 0 until temps.size) {
attName?.setText((i+1).toString())
attVal?.setText(temps.get(i).toString())
table?.addView(row)
}
table?.requestLayout()
}
}
В первом клике ничего не отображается.
and in the second clik it crashes and shows:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tharwa.tdm2_exo2, PID: 10114
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:4454)
at android.view.ViewGroup.addView(ViewGroup.java:4295)
at android.widget.TableLayout.addView(TableLayout.java:426)
at android.view.ViewGroup.addView(ViewGroup.java:4235)
at android.widget.TableLayout.addView(TableLayout.java:408)
at android.view.ViewGroup.addView(ViewGroup.java:4208)
at android.widget.TableLayout.addView(TableLayout.java:399)
at com.tharwa.tdm2_exo2.TableAgent.TablePresentation.updateTable(TablePresentation.kt:63)
...
Я не понял, как сначала позвонить removeView()
родителю ребенка и как заставить его отображаться динамически.