Android ListView - add dynamic row to row -
in listview
have 3 row types, txt, img, sms. each 1 has different row layout , works fine this:
... extends baseadapter ... public view getview(int position, view convertview, viewgroup parent) { textviewholder textviewholder = null; imageholder imageholder = null; smsholder smsholder = null; //placeholder placeholder = null; conversionmodel conversion = getitem(position); int type = conversion.type; if (convertview == null) { if(type == conversionmodel.txt) { textviewholder = new textviewholder(); convertview = minflater.inflate(r.layout.convers_txt, null); textviewholder.textview = (textview)convertview.findviewbyid(r.id.row_txt); convertview.settag(textviewholder); } if(type == conversionmodel.img) { imageholder = new imageholder(); convertview = minflater.inflate(r.layout.convers_img, null); imageholder.img = (imageholder)convertview.findviewbyid(r.id.row_txt); convertview.settag(textviewholder); } .... } ... }
my purpose this:
-alllist----------- ----txt------------ ----img------------ -----(multipledata) //placeholder must has dynamic rows in --------button----- --------button----- ---sms------------- .....
my placeholder must has dynamic rows in because don't know numbers of data comes server. tried nested listview shows first data in list.
also put linearlayout
placeholder
, added items this(pseudo):
for data in datas { button btn = new button(ctx) ... placeholder.layout.addview(btn); }
but whenever scroll listview
, getview
method messing , it's rendering same buttons 10-20 times, when should 2-3.
how can achieve this? looked @ app sources telegram couldn't found similar approaches.
solution:
string arr[] = conversion.message.split(pattern.quote("$$")); linearlayout layholder = new linearlayout(ctx); (int = 0; < arr.length; i++) { button test_btn = new button(ctx); //placemodel pm = new placemodel(arr[i]); layholder.addview(test_btn); } placeholder.linearlay.removeallviews(); //<- placeholder.linearlay.addview(layholder);
add linear layout in child layout this:(for demo have added 1 button in odd child , 2 buttons in child position)
... <!--main linear layout contnet--> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview" /> <linearlayout android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" />
and add buttons in it:
in getview()
... linearlayout layout = (linearlayout) view.findviewbyid(r.id.layout); //important line layout.removeallviews(); if (position % 2 == 0) { button b1 = new button(_context); b1.settext("b1"); layout.addview(b1); } else { button b1 = new button(_context); b1.settext("b1"); layout.addview(b1); button b2 = new button(_context); b2.settext("b2"); layout.addview(b2); }
Comments
Post a Comment