android - Create dyanamic List Row layout Programatically -


thanks reading query.

we looking template based solution list row each list row might have different-2 field , alignment driven server in form of json.

this json have information no of fields , type of fields label, image , alignment

we came solution each row, in getview() method, parsing layout json , creating field programatically , adding corresponding values , adding relative layout , adding these relative layout parent linear layout vertical orientation. assigning layout convertview , way, row created.

with solution list view not smooth, because lots of calculation happening generate dynamic fields.

any appriciated. below code sample layout json

{   "layouts": [   {   "templateid": "hci",   "width": "100%",   "backgroundcolor": "#87cefa",   "selectedbackgroundcolor": "#96f2cd",   "cornerradius": "0",   "borderwidth": "1",   "bordercolor": "#000000",   "children": [     {       "width": "100%",       "height": "20",       "rightalign": "0",       "children": [         {           "type": "label",           "field": "claimantname",           "textcolor": "#eb6208",           "alignment": "left",           "fontsize": "16",           "style": "upper",           "horizontalpadding": "5",           "bold": "1"         },         {           "type": "label",           "field": "worktype",           "textcolor": "#eb6208",           "alignment": "left",           "fontsize": "16",           "style": "upper",           "horizontalpadding": "5",           "bold": "0"         },         {           "type": "label",           "field": "workkardnumber",           "backgroundcolor": "#2d5db9",           "textcolor": "#ffffff",           "alignment": "right",           "horizontalpadding": "3"         }       ]     },     {       "width": "100%",       "height": "15",       "children": [         {           "type": "label",           "field": "address",           "textcolor": "#458b00",           "alignment": "left",           "fontsize": "16",           "style": "upper",           "horizontalpadding": "5",           "bold": "1"         },         {           "type": "label",           "field": "lossdate",           "textcolor": "#838b83",           "alignment": "",           "fontsize": "16",           "style": "lower",           "horizontalpadding": "5",           "bold": "0"         }       ]     },     {       "width": "100%",       "height": "15",       "children": [         {           "type": "label",           "field": "county",           "textcolor": "#838b83",           "alignment": "left",           "fontsize": "16",           "style": "upper",           "horizontalpadding": "5",           "bold": "0"         },         {           "type": "label",           "field": "zip",           "textcolor": "#0000ff",           "alignment": "",           "fontsize": "16",           "style": "lower",           "horizontalpadding": "5",           "bold": "0"         },         {           "type": "label",           "field": "state",           "textcolor": "#458b00",           "alignment": "",           "fontsize": "16",           "style": "upper",           "horizontalpadding": "5",           "bold": "0"         },         {           "type": "label",           "field": "reporteddate",           "textcolor": "#838b83",           "alignment": "",           "fontsize": "16",           "style": "lower",           "horizontalpadding": "5",           "bold": "0"         }       ]     },     {       "width": "100%",       "height": "50",       "children": [         {           "type": "spacer",           "width": "20"         },         {           "type": "label",           "field": "losstype",           "style": "upper",           "width": "280",           "textcolor": "#458b00",           "bold": "1",           "fontsize": "16"         }       ]     },     {       "width": "100%",       "height": "20",       "children": [        ]     }   ] } 

] }

and below logic parse , dynamic field creation

public linearlayout generatelayout(object object, workkarddata workkard){     this.workkarddata=workkard;     jsonobject json = (jsonobject)object;     string template="";     string cardbackground="";     string cardselectedbackground="";     arraylist child=new arraylist();     arraylist subchild=new arraylist();     string fieldname="";     try {         template = json.getstring("templateid");     } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     }     try {         cardbackground = json.getstring("backgroundcolor");     } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     }     try {         cardselectedbackground = json.getstring("selectedbackgroundcolor");     } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     }     try {         cardbackground = json.getstring("backgroundcolor");     } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     }      linearlayout workkardrowlayout = new linearlayout(context);     workkardrowlayout.setorientation(linearlayout.vertical);     workkardrowlayout.setbackgroundcolor(color.parsecolor(cardbackground));      try {         jsonarray childrentop = json.getjsonarray("children");         child.add(childrentop);         (int = 0; < childrentop.length(); i++) {             jsonarray subchildren = childrentop.getjsonobject(i).getjsonarray("children");             subchild.add(subchildren);              string width="350", height="0", rightalignmen="0";             int widthint=0;             try {                 width = childrentop.getjsonobject(i).getstring("width");             } catch (exception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             try {                 height = childrentop.getjsonobject(i).getstring("height");             } catch (exception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             try {                 rightalignmen = childrentop.getjsonobject(i).getstring("rightalign");             } catch (exception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }              if(width.equalsignorecase("100%")){                 widthint = 350;             }else{                 widthint = 350*(integer.parseint(width.trim().split("%")[0]))/100;             }              linearlayout.layoutparams paramsll1 = new linearlayout.layoutparams(widthint,integer.parseint(height.trim()));             /*if(rightalignmen.equalsignorecase("1")){                                                                             paramsll1.gravity = gravity.right;                                                             }else{                                                                             paramsll1.gravity = gravity.left;                                                              }*/              linearlayout listlayoutrow1 = new linearlayout(context);             ll[i] = new linearlayout(context);             ll[i].setorientation(linearlayout.horizontal);             ll[i].setweightsum(10);             ll[i].setlayoutparams(paramsll1);              if(subchildren.length()==0){                 workkardrowlayout.addview(ll[i]);             }else{                 (int j = 0; j < subchildren.length(); j++) {                      string type = "", field= "", backgroundcolor ="", textcolor ="", alignment ="",horizontalpadding ="", bold="",widthchild="",textcolorchild="",fontsize="",style="lower";                     try {                         type = subchildren.getjsonobject(j).getstring("type");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         field = subchildren.getjsonobject(j).getstring("field");                         fieldname=field;                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         backgroundcolor = subchildren.getjsonobject(j).getstring("backgroundcolor");                         //ll[i].setbackgroundcolor(color.parsecolor(backgroundcolor));                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         textcolor = subchildren.getjsonobject(j).getstring("textcolor");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         alignment = subchildren.getjsonobject(j).getstring("alignment");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         horizontalpadding = subchildren.getjsonobject(j).getstring("horizontalpadding");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         bold = subchildren.getjsonobject(j).getstring("bold");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                      try {                         widthchild = subchildren.getjsonobject(j).getstring("width");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         textcolorchild = subchildren.getjsonobject(j).getstring("textcolor");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         fontsize = subchildren.getjsonobject(j).getstring("fontsize");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                     try {                         style = subchildren.getjsonobject(j).getstring("style");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                       int padding=0;                     try {                         padding = integer.parseint(horizontalpadding);                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                      linearlayout.layoutparams llchild = new linearlayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content);                     //paramsll1. = integer.parseint(horizontalpadding);                     //paramsll1.topmargin = 10;                     //paramsll1.bottommargin = 10;                     //paramsll1.rightmargin = 10;                     //paramsll1.gravity = gravity.left;                     if(rightalignmen.equalsignorecase("1")){                         llchild.gravity = gravity.right;                     }                      if(type.equalsignorecase("label")){                         textview listtext1 = new textview(context);                         if(style.equalsignorecase("upper")){                             //listtext1.settypeface(null, typeface.bold);                             listtext1.settext(this.getworkkardmethod(field).touppercase());                         }else{                             listtext1.settext(this.getworkkardmethod(field));                         }                         if(field.equalsignorecase("lossdate")){                             listtext1.settext("loss date:"+this.getworkkardmethod(field));                         }                         if(field.equalsignorecase("reporteddate")){                             listtext1.settext("reported:"+this.getworkkardmethod(field));                         }                         if (!backgroundcolor.equalsignorecase("")) {                             listtext1.setbackgroundcolor(color.parsecolor(backgroundcolor));                         }                         listtext1.settextcolor(color.parsecolor(textcolor));                         if (!fontsize.equalsignorecase("")) {                             listtext1.settextsize(integer.parseint(fontsize));                         }                         listtext1.setpadding(padding,padding, padding, padding);                         if(alignment.equalsignorecase("left")){                             ll[i].setgravity(gravity.left);                         }                         if(alignment.equalsignorecase("right")){                             ll[i].setgravity(gravity.right);                         }                         if(alignment.equalsignorecase("centre")){                             ll[i].setgravity(gravity.center);                         }                         if(bold.equalsignorecase("1")){                             listtext1.settypeface(null, typeface.bold);                         }                         ll[i].addview(listtext1);                         ll[i].setlayoutparams(llchild);                     }else if(type.equalsignorecase("spacer")){                         llchild.width=integer.parseint(widthchild);                         ll[i].setlayoutparams(llchild);                         textview listtext1 = new textview(context);                         listtext1.settext(" ");                         ll[i].addview(listtext1);                     }                 }                 workkardrowlayout.addview(ll[i]);             }          }     } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     }        //                            linearlayout workkardrowlayout = new linearlayout(context);     //                            workkardrowlayout.setorientation(linearlayout.vertical);     //                            workkardrowlayout.setbackgroundcolor(color.parsecolor(cardbackground));      //if()      //                            linearlayout listlayoutrow1 = new linearlayout(context);     //                            linearlayout listlayoutrow2 = new linearlayout(context);     //                            linearlayout listlayoutrow3 = new linearlayout(context);     //     //                            listlayoutrow1.setorientation(linearlayout.horizontal);     //                            listlayoutrow2.setorientation(linearlayout.horizontal);     //                            listlayoutrow3.setorientation(linearlayout.horizontal);     //     //                            listlayoutrow1.setweightsum(20);     //                            listlayoutrow2.setweightsum(20);     //                            listlayoutrow3.setweightsum(20);     //     //                            linearlayout.layoutparams paramsll1 = new linearlayout.layoutparams(300, layoutparams.wrap_content);     //                            paramsll1.leftmargin = 10;     //                            paramsll1.topmargin = 10;     //                            paramsll1.bottommargin = 10;     //                            paramsll1.rightmargin = 10;     //                            paramsll1.gravity = gravity.left;     //                            listlayoutrow1.setlayoutparams(paramsll1);     //                            listlayoutrow2.setlayoutparams(paramsll1);     //                            listlayoutrow3.setlayoutparams(paramsll1);     //     //                            textview listtext1 = new textview(context);     //                            listtext1.settext(template);     //                            textview listtext2 = new textview(context);     //                            listtext2.settext("hurre............ dynamic lauout row2:-)");     //                            textview listtext3 = new textview(context);     //                            listtext3.settext("hurre............ dynamic lauout row 3:-)");     //     //                            listlayoutrow1.addview(listtext1);     //                            listlayoutrow2.addview(listtext2);     //                            listlayoutrow3.addview(listtext3);     //     //                            workkardrowlayout.addview(listlayoutrow1);     //                            workkardrowlayout.addview(listlayoutrow2);     //                            workkardrowlayout.addview(listlayoutrow3);      return workkardrowlayout;  }  string getworkkardmethod(string fieldname){     string work="";     if(fieldname.equalsignorecase("template")){         work=workkarddata.gettemplate();     }else if(fieldname.equalsignorecase("workkardtitle")){         work=workkarddata.getworkkardtitle();     }else if(fieldname.equalsignorecase("workkarddescription")){         work=workkarddata.getworkkarddescription();     }else if(fieldname.equalsignorecase("worktype")){         work=workkarddata.getworktype();     }else if(fieldname.equalsignorecase("workkardnumber")){         work=workkarddata.getworkkardnumber();     }else if(fieldname.equalsignorecase("claimantname")){         work=workkarddata.getclaimantname();     }else if(fieldname.equalsignorecase("reporteddate")){         work=workkarddata.getreporteddate();     }else if(fieldname.equalsignorecase("lossdate")){         work=workkarddata.getlossdate();     }else if(fieldname.equalsignorecase("losstype")){         work=workkarddata.getlosstype();     }else if(fieldname.equalsignorecase("address")){         work=workkarddata.getaddress();     }else if(fieldname.equalsignorecase("county")){         work=workkarddata.getcounty();     }else if(fieldname.equalsignorecase("state")){         work=workkarddata.getstate();     }else if(fieldname.equalsignorecase("zip")){         work=workkarddata.getzip();     }      return work; } 

here things can improve view's performance:

  1. reuse views(you inflating view every single instance). if have variable number of fields adapter.getviewtype , adapter.getviewtypecount friends.
  2. findviewbyid's expensive , make less performant experience. read on viewholder pattern.
  3. if still running problems need @ causing poor performance. hunch if still have poor performance after doing 1 , 2 it's parsing json in getview. consider preparsing data objects can read once getview called.

supplemental video(long useful adapters in general).


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -