java - How to add an item in Listview adpater Linear Layout -
i know how populate listview , know how make custom adapter. want add view inside linear layout of rowlayout of listview. here mylist adapter.
i want listview each item layout created runtime can not achieve this.
this code working without listview when add linearlayout inside listview .it gives me error.
public class kmaplistadapter extends baseadapter { context context; layoutinflater layoutinflater; list<kmapmodel> moriginalvalues = new arraylist<kmapmodel>(); public kmaplistadapter(context activity, list<kmapmodel> kmapmodellist) { this.context = activity; this.moriginalvalues = kmapmodellist; } @override public int getcount() { if(moriginalvalues == null) return 0; return moriginalvalues.size(); } @override public object getitem(int position) { return moriginalvalues.get(position); } @override public long getitemid(int position) { return position; } viewholder holder ; @override public view getview(int position, view convertview, viewgroup parent) { kmapmodel kmapmodel; if (layoutinflater == null) layoutinflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) { holder = new viewholder(); convertview = layoutinflater.inflate(r.layout.rowlayout, null); holder.linearlayout = (linearlayout) convertview.findviewbyid(r.id.linear_layout_matrix_1); convertview.settag(holder); } else holder = (viewholder) convertview.gettag(); kmapmodel = moriginalvalues.get(position); holder.linearlayout.addview(kmapmodel.getlinearlayout()); return convertview; } static class viewholder { linearlayout linearlayout;}}
and model list
public list<kmapmodel> listviewoneitem( int rows, int cols) { string[][] kmaparray = kmapmaker(rows, cols); // log.v("kmap array",""+kmaparray); textview[][] kmaptextview = new textview[kmaparray.length][kmaparray[0].length]; int temp = 3; (int = 0; < kmaparray.length; i++) { kmapmodel kmapmodel = new kmapmodel(); linearlayout rowlinearlayout = makerowlinearlayout(); (int j = 0; j < kmaparray[0].length; j++) { textview rowtextview = maketextview(); rowtextview.setpadding(4, 4, 4, 4); rowlinearlayout.addview(rowtextview); kmaptextview[i][j] = rowtextview;// add text view in jagged array later use } kmapmodel.setlinearlayout(rowlinearlayout); kmapmodellist.add(kmapmodel); } return kmapmodellist; }
now got error
the specified child has parent. must call removeview() on child's parent first. on line
holder.linearlayout.addview(kmapmodel.getlinearlayout());
q1) how resolve problem q2) why getting issue ?
thanks @pskink give me way try upload solution.
the specified child has parent.
stop trying give child second parent. views broken homes. have 1 parent.
you can add views views, cannot add views added. causes loops , loops loopy. , iterate viewtree needs tree. views 1 , 1 parent. either clear out layout have , use or add new view layout or want doesn't involve using same object in several places in view tree.
Comments
Post a Comment