I have a table and tablerow which has 5 textview. I created tablelayout and tablerow in myfragment.xml and I want to add tablerow to tablelayout programmaticaly. But I am got Illegal State Exception during all day.
Here is my table and tablerow xml code(myfragment.xml):
<LinearLayout tools:context="com.xxx.xx.Fragment.myfragment"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent" android:layout_width="match_parent"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/lineer">
<!-- a Spinner -->
<!-- a Textview -->
<TableLayout
android:id="@+id/mytable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:shrinkColumns="*">
<TableRow android:id="@+id/rowtest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/table_one"
android:layout_weight="1" />
<TextView
android:id="@+id/table_two"
android:layout_weight="1" />
<TextView
android:id="@+id/table_three"
android:layout_weight="1"/>
<TextView
android:id="@+id/table_four"
android:layout_weight="1"/>
<TextView
android:id="@+id/table_five"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</LinearLayout>
I want to create tablerow dynamically and add it to tablelayout when user choose something from spinner. But I am getting Illegal State Exception when I choose something from Spinner. I think my textviews are creating this problem. Here is android codes:
TextView tv,tv2,tv3,tv4,tv5;
TableLayout detailsTable;
TableRow tableRow;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment, container, false);
detailsTable = (TableLayout) view.findViewById(R.id.mytable);
tableRow = (TableRow) view.findViewById(R.id.rowtest);
tv = (TextView) view.findViewById(R.id.table_one);
tv2 = (TextView) view.findViewById(R.id.table_two);
tv3 = (TextView) view.findViewById(R.id.table_three);
tv4 = (TextView) view.findViewById(R.id.table_four);
tv5 = (TextView) view.findViewById(R.id.table_five);
return view;
}
I call my createrow() method in spinner's onitemselected() method. I have a createrow() method:
private void createRow(String da, String hn,String krd,String puan, String snc) {
tv.setText(da);
denemerow.addView(tv);
tv2.setText(hn);
denemerow.addView(tv2);
tv3.setText(krd);
denemerow.addView(tv3);
tv4.setText(puan);
denemerow.addView(tv4);
tv5.setText(snc);
denemerow.addView(tv5);
detailsTable.addView(denemerow);
}
My logcat:
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:3880)
at android.view.ViewGroup.addView(ViewGroup.java:3733)
at android.view.ViewGroup.addView(ViewGroup.java:3678)
at android.view.ViewGroup.addView(ViewGroup.java:3654)
at com.xx.xx.Fragment.myfragment.createRow(myfragment.java:284)
at com.xx.xx.Fragment.myfragment.onItemSelected(myfragment.java:356)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:897)
at android.widget.AdapterView.access$200(AdapterView.java:48)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:865)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Where I am missing?