android - How to set color code as per Json Object -


i have following json response,i trying set color per displytext,but set last color code whole string,

json response

java code

 ch_list = new arraylist<string>();                         color_list=new arraylist<string>();                          try {                              (int = 0; < response.length(); i++) {                                  jsonobject person = (jsonobject) response                                         .get(i);                                  system.out.println("person"+person);                                  string searchcode = person.getstring("searchcode");                                 system.out.println("searchcode"+searchcode);                                      jsonarray ja = person.getjsonarray("itemlist");                                 (int j = 0; j < ja.length(); j++) {                                       jsonobject jo = ja.getjsonobject(j);                                      searchresultmodel ch = new searchresultmodel();                                     ch.setsearch_name(jo.getstring("productname"));                                     ch.setsearch_img(jo.getstring("productimage"));                                     ch.setsearch_imgloc(jo.getstring("productimageloc"));                                     ch.setsearch_price(jo.getstring("productsp"));                                     ch.setsearch_ratingimg(jo.getstring("pratingimgname"));                                     ch.setsearch_ratingimgpath(jo.getstring("pratingimgpath"));                                      jsonarray txtdetail=jo.getjsonarray("productoffertext");                                      system.out.println("txtdetailarray"+txtdetail);                                      stringbuilder sb = new stringbuilder();                                     for(int k=0;k<txtdetail.length();k++)                                     {                                         jsonobject jdetail=txtdetail.getjsonobject(k);                                            disptext=jdetail.getstring("displaytext");                                         dispclr=jdetail.getstring("displaycolor");                                         ch_list.add(disptext);                                         color_list.add(dispclr);                                         sb.append(disptext);                                          system.out.println("clr" + color_list.get(k).tostring());                                         colors=color_list.get(k);                                           string string = colors;                                         string[] parts = string.split("\\*");                                         int part1 = integer.parseint(parts[0]);                                         int part2 = integer.parseint(parts[1]);                                         int part3 = integer.parseint(parts[2]);                                            hex = string.format("#%02x%02x%02x", part1, part2, part3);                                         system.out.println("hexa"+hex);                                         // system.out.println("textnames" + ch_list.get(k).tostring());                                       }                                         detailtext=sb.tostring().replaceall("\\\\n", "\n");                                    //  system.out.println("gh"+sb.tostring()+detailtext);                                     system.out.println("output: " + sb.tostring().replaceall("\\\\n", "\n"));                                      searchlist.add(ch);                                  }                               } 

adapter

public class customlistadaptercountry extends baseadapter {         private  aquery aquery;         private activity activity;         private layoutinflater inflater;         private list<searchresultmodel> movieitems;         private list<string> dispitems;          imageloader imageloader = myapplication.getinstance().getimageloader();           public customlistadaptercountry(activity activity, list<searchresultmodel> movieitems,arraylist<string> dispitems) {             this.activity = activity;             this.movieitems = movieitems;             this.dispitems = dispitems;             aquery = new aquery(this.activity);         }          @override         public int getcount() {             return movieitems.size();         }          @override         public object getitem(int location) {             return movieitems.get(location);         }          @override         public long getitemid(int position) {             return position;         }          @override         public view getview(int position, view convertview, viewgroup parent) {              if (inflater == null)                 inflater = (layoutinflater) activity                         .getsystemservice(context.layout_inflater_service);             if (convertview == null)                 convertview = inflater.inflate(r.layout.list_item_searchresult, null);              if (imageloader == null)                 imageloader = myapplication.getinstance().getimageloader();              networkimageview iv = (networkimageview) convertview                     .findviewbyid(r.id.search_image);             imageview ratingiv = (imageview) convertview                     .findviewbyid(r.id.search_rating);               textview title = (textview) convertview.findviewbyid(r.id.search_title);             textview price = (textview) convertview.findviewbyid(r.id.search_price);             textview dettext = (textview) convertview.findviewbyid(r.id.search_detailtext);             // getting movie data row             searchresultmodel m = movieitems.get(position);                 iv.setimageurl(m.getsearch_imgloc() + m.getsearch_img(), imageloader);              aquery.id(ratingiv).image(m.getsearch_ratingimgpath() + m.getsearch_ratingimg(), true, true, 0, r.mipmap.ic_launcher);               //log.d("searchimgs", joined);              // title             title.settext(m.getsearch_name());             price.settext("$"+m.getsearch_price());             dettext.settext(detailtext);                dettext.settextcolor(color.parsecolor(hex));              return convertview;         }      } 

you can try following method. format text html , use display.

private string addcolor(string str, string hexcolor) {     string html = "";     if(str.contains("\\\\n"))     {       html = "</br>";       str.replaceall("\\\\n", "");     }     html = html + "<font color='"+hexcolor+"'>"+str+"</font>";     return html; } 

delete sb.append(disptext); right , add sb.append(addcolor(disptext, hex)); after hex = string.format("#%02x%02x%02x", part1, part2, part3);

replace dettext.settext(detailtext); dettext.settext(html.fromhtml(detailtext)); , delete dettext.settextcolor(color.parsecolor(hex));.

this should job.

also, not using arraylists ch_list , color_list can used task.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -