Adding Table rows Dynamically in Android -
i trying create layout need add table rows dynamically. below table layout xml
<tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/displaylinear" android:background="@color/background_df" android:orientation="vertical" > <tablerow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/display_row" android:layout_margintop="280dip" > </tablelayout>
the activity file rows being added dynamically is
public void init(){ menudb = new menudbadapter(this); ll = (tablelayout) findviewbyid(r.id.displaylinear); tablerow row=(tablerow)findviewbyid(r.id.display_row); (int = 0; <2; i++) { checkbox = new checkbox(this); tv = new textview(this); addbtn = new imagebutton(this); addbtn.setimageresource(r.drawable.add); minusbtn = new imagebutton(this); minusbtn.setimageresource(r.drawable.minus); qty = new textview(this); checkbox.settext("hello"); qty.settext("10"); row.addview(checkbox); row.addview(minusbtn); row.addview(qty); row.addview(addbtn); ll.addview(row,i); } }
but when run this, getting below error
08-13 16:27:46.437: e/androidruntime(23568): java.lang.runtimeexception: unable start activity componentinfo{com.example.roms/com.example.roms.displayactivity}: java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first.
i understand due command ll.addview(row,i);
when remove adding stuff in single row rather tan creating new row next item. tried giving index row.addview(addbtn,i)
still not populating correctly. please advise. thanks.
you shouldn't using item defined in layout xml in order create more instances of it. should either create in separate xml , inflate or create tablerow programmaticaly. if creating them programmaticaly, should this:
public void init(){ tablelayout ll = (tablelayout) findviewbyid(r.id.displaylinear); (int = 0; <2; i++) { tablerow row= new tablerow(this); tablerow.layoutparams lp = new tablerow.layoutparams(tablerow.layoutparams.wrap_content); row.setlayoutparams(lp); checkbox = new checkbox(this); tv = new textview(this); addbtn = new imagebutton(this); addbtn.setimageresource(r.drawable.add); minusbtn = new imagebutton(this); minusbtn.setimageresource(r.drawable.minus); qty = new textview(this); checkbox.settext("hello"); qty.settext("10"); row.addview(checkbox); row.addview(minusbtn); row.addview(qty); row.addview(addbtn); ll.addview(row,i); } }
Comments
Post a Comment