java - Data received from Activity bundle is not being displayed on BarChart in Fragment -


i making app i'm making api call data through volley , set parcelable arraylist , send parcelable arraylist through bundle fragment contains barchart used mpchart library.

problem: data not populated in barchart. static data populating in graph not dynamic data (parcelable arraylist) parse so, i'm wondering if have somehow notify adapter or data set being changed. don't know i'm going wrong. appreciated have been stuck since yesterday.

i tried refresh data in viewpager fragment

main activity

    public class mainactivity extends appcompatactivity implements viewpager.onpagechangelistener {      radiogroup reports_criteria;     edittext sdate, edate;     textview reportsdate;     string url = "https://xyxyxyx.php/api/performance_api/graphchart";     string db_name = "xxxxxx", user_id = "xxxxx", from_date = "xxxxx", to_date = "xxxx", comp_fk = "xxxxx";     requestqueue queue;     public arraylist<performancedata> pdata;     private int myear, mmonth, mday;     public fragmentmanager fragmentmanager;     public viewpager pager;     public static pageadapter a;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          fragmentmanager = getsupportfragmentmanager();          queue = volley.newrequestqueue(this);          pdata = new arraylist<>();          performanceapicall();          initui();          radiogrouplistener();          clicklisteners();          pager = (viewpager) findviewbyid(r.id.pager);         //pager.setoffscreenpagelimit(5);          = new pageadapter(getsupportfragmentmanager(), this.getapplicationcontext());         pager.setadapter(a);         pager.addonpagechangelistener(this);       }      private void radiogrouplistener() {         reports_criteria.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {             @override             public void oncheckedchanged(radiogroup radiogroup, @idres int i) {                 radiobutton rb = (radiobutton) findviewbyid(i);                 if (null != rb && > -1) {                     toast.maketext(mainactivity.this, rb.gettext(), toast.length_short).show();                 }             }         });     }      private void performanceapicall() {          stringrequest request = new stringrequest(request.method.post, url, new response.listener<string>() {              @override             public void onresponse(string response) {                 log.d("response siddhi: ", response.tostring());                  try {                     jsonarray jsonarray = new jsonarray();                     jsonobject jobj = new jsonobject(response);                     jsonobject prodobj = new jsonobject();                      string fromdate = jobj.getstring("fromdate");                     string todate = jobj.getstring("todate");                     prodobj = jobj.getjsonobject("productivity");                     jsonarray = prodobj.getjsonarray("array");                     system.out.println("json: " + jsonarray);                      if (jsonarray.length() != 0) {                         (int = 0; < jsonarray.length(); i++) {                             string name = jsonarray.getjsonobject(i).getstring("name");                             log.e("name ", name);                             jsonarray array = jsonarray.getjsonobject(i).getjsonarray("data");                             arraylist<string> data;                             if (array.length() != 0) {                                 data = new arraylist<>();                                 (int j = 0; j < array.length(); j++) {                                     log.e("data_val", " " + array.get(j));                                     data.add(array.get(j).tostring());                                 }                                 performancedata performancedata = new performancedata(fromdate, todate, name, data);                                 system.out.println("data after setting performance " + performancedata.getfrom_date() + performancedata.getto_date() + performancedata.getname() + performancedata.getdata());                                 pdata.add(performancedata);                             }                         }                     }                    // getsupportfragmentmanager().begintransaction().remove(a.getitem(1));                     productivity prod = new productivity();                     bundle b = new bundle();                     b.putparcelablearraylist("pdata",pdata);                     prod.setarguments(b);                     getsupportfragmentmanager().begintransaction().add(prod, null).commit();                     pager.getadapter().notifydatasetchanged();                     //getsupportfragmentmanager().begintransaction().remove(a.getitem(1));                   //  pager.getadapter().notifydatasetchanged();                 } catch (jsonexception e) {                     log.d("jsonexception ", e.tostring());                 }             }         }, new response.errorlistener() {              @override             public void onerrorresponse(volleyerror error) {                 log.d("error siddhi ", error.getmessage());             }         }) {             @override             protected map<string, string> getparams() throws authfailureerror {                  map<string, string> mparams = new hashmap<>();                 mparams.put(db_name, "xyxyxyxy");                 mparams.put(user_id, "000");                 mparams.put(from_date, "0000-00-00");                 mparams.put(to_date, "0000-00-00");                 mparams.put(comp_fk, "00");                  return mparams;             }         };         queue.add(request);     }      private void clicklisteners() {         view.onclicklistener listener = new view.onclicklistener() {             @override             public void onclick(view view) {                  switch (view.getid()) {                     case r.id.et_graph_startdate:                         selectstartdate();                         break;                     case r.id.et_graph_enddate:                         selectenddate();                         break;                 }             }         };         sdate.setonclicklistener(listener);         edate.setonclicklistener(listener);     }      private void selectenddate() {         final calendar c = calendar.getinstance();         myear = c.get(calendar.year);         mmonth = c.get(calendar.month);         mday = c.get(calendar.day_of_month);          datepickerdialog datepickerdialog = new datepickerdialog(this, new datepickerdialog.ondatesetlistener() {             @override             public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) {                 edate.settext(year + "-" + (monthofyear + 1) + "-" + dayofmonth);             }         }, myear, mmonth, mday);         datepickerdialog.show();     }      private void selectstartdate() {         final calendar c = calendar.getinstance();         myear = c.get(calendar.year);         mmonth = c.get(calendar.month);         mday = c.get(calendar.day_of_month);          datepickerdialog datepickerdialog = new datepickerdialog(this, new datepickerdialog.ondatesetlistener() {             @override             public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) {                 sdate.settext(year + "-" + (monthofyear + 1) + "-" + dayofmonth);             }         }, myear, mmonth, mday);         datepickerdialog.show();     }      private void initui() {          reports_criteria = (radiogroup) findviewbyid(r.id.rg_criteria);          sdate = (edittext) findviewbyid(r.id.et_graph_startdate);         edate = (edittext) findviewbyid(r.id.et_graph_enddate);          reportsdate = (textview) findviewbyid(r.id.tv_reports_date);     }      @override     public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {      }      @override     public void onpageselected(int position) {          fragment fragment = ((pageadapter)pager.getadapter()).getfragment(position);          if(position == 1 && fragment != null) {             fragment.onresume();         }      }      @override     public void onpagescrollstatechanged(int state) {      } } 

performance data

    public class performancedata implements parcelable {      private string from_date, to_date, name;     private arraylist<string> data;      public performancedata(string fromdate, string todate, string name, arraylist<string> data) {          this.from_date = fromdate;         this.to_date = todate;         this.name = name;         this.data = data;      }      protected performancedata(parcel in) {         from_date = in.readstring();         to_date = in.readstring();         name = in.readstring();         data = in.createstringarraylist();     }      public static final creator<performancedata> creator = new creator<performancedata>() {         @override         public performancedata createfromparcel(parcel in) {             return new performancedata(in);         }          @override         public performancedata[] newarray(int size) {             return new performancedata[size];         }     };      @override     public int describecontents() {         return 0;     }      @override     public void writetoparcel(parcel parcel, int i) {         parcel.writestring(from_date);         parcel.writestring(to_date);         parcel.writestring(name);         parcel.writestringlist(data);     }      public string getfrom_date() {         return from_date;     }      public string getto_date() {         return to_date;     }      public string getname() {         return name;     }      public arraylist<string> getdata() {         return data;     } } 

page adapter

    public class pageadapter extends fragmentstatepageradapter {     private map<integer, string> mfragmenttags;     private fragmentmanager mfragmentmanager;     private context mcontext;      public pageadapter(fragmentmanager fm, context context) {         super(fm);         mfragmentmanager = fm;         mfragmenttags = new hashmap<integer, string>();         mcontext = context;     }      @override     public object instantiateitem(viewgroup container, int position) {          object obj = super.instantiateitem(container, position);         if (obj instanceof fragment) {              fragment f = (fragment) obj;             string tag = f.gettag();             mfragmenttags.put(position, tag);         }         return obj;     }      public fragment getfragment(int position) {         string tag = mfragmenttags.get(position);         if (tag == null)             return null;         return mfragmentmanager.findfragmentbytag(tag);     }      @override     public fragment getitem(int position) {          log.e("frag_position", "" + position);         switch (position) {             case 0:                 return new consolidated();             case 1:                 return new productivity();             /*case 2:                 return new taskratio();             case 3:                 return new performancequality();             case 4:                 return new performancedelivery();             case 5:                 return new pastdue();             case 6:                 return new taskdeliveryratio();             case 7:                 return new cycletime();             case 8:                 return new leadtime();*/             default:                 return null;         }     }      @override     public int getcount() {         return 2;     }      @override     public int getitemposition(object object) {         return position_none;     }  } 

productivity fragment

    public class productivity extends fragment {      protected barchart barchart;     public arraylist<performancedata> prod_array;     public arraylist<string> task_name;     public string tag = "productivity", fromdate, todate;     public static float[] ft, tt, im, em;     //public static float[] f, t, im_, e;     public static arraylist<barentry> barentries;       @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         setretaininstance(true);         view v = inflater.inflate(r.layout.frag_productivity, container, false);          barentries = new arraylist<>();          /*f = new float[] {6f,8f,9f,7f};         t = new float[] {9f,4f,5.5f,3.5f};         im_ = new float[] {4.5f,7f,7f,9f};         e = new float[] {2f,3f,5f,1f};*/          parseproductivitydata();          barchart = v.findviewbyid(r.id.chart1);          log.e("entry_size", "" + barentries.size());         // if(barentries.size()!=0){         barchart = v.findviewbyid(r.id.chart1);         barchart.getdescription().setenabled(false);         barchart.setdrawgridbackground(false);          // scaling can done on x- , y-axis separately         barchart.setpinchzoom(true);         barchart.setdrawbarshadow(false);         barchart.setdrawvalueabovebar(false);         barchart.sethighlightfullbarenabled(false);          // change position of y-labels bar chart         yaxis leftaxis = barchart.getaxisleft();         leftaxis.setdrawaxisline(false);         leftaxis.setdrawlabels(false);         leftaxis.setdrawgridlines(false);         leftaxis.setaxisminimum(0f); //          xaxis xlabels = barchart.getxaxis();         xlabels.setdrawlabels(false);         xlabels.setdrawgridlines(false);         xlabels.setdrawaxisline(false);          barchart.setdata(getproductivitybarchartdata(barchart));         barchart.animatexy(3000, 3000);          legend l = barchart.getlegend();         legendentry clable1 = new legendentry("free time", legend.legendform.default, 8f, 0, null, colortemplate.material_colors[0]);         legendentry clable2 = new legendentry("tasks", legend.legendform.default, 8f, 0, null, colortemplate.material_colors[1]);         legendentry clable3 = new legendentry("internal meeting", legend.legendform.default, 8f, 0, null, colortemplate.material_colors[2]);         legendentry clable4 = new legendentry("external meeting", legend.legendform.default, 8f, 0, null, colortemplate.material_colors[3]);         l.setcustom(new legendentry[]{clable1, clable2, clable3, clable4});          barchart.getaxisright().setenabled(false);          xaxis xaxis = barchart.getxaxis();         xaxis.setenabled(false);          return v;     }      @override     public void onresume() {         parseproductivitydata();         barchart.notifydatasetchanged();         barchart.invalidate();         super.onresume();     }      private void parseproductivitydata() {          bundle b = getarguments();         if (b != null) {             prod_array = b.getparcelablearraylist("pdata");             log.e(tag, "oncreateview: successfull" + prod_array.size());              todate = prod_array.get(0).getto_date();             fromdate = prod_array.get(0).getfrom_date();             task_name = new arraylist<>();             log.e(tag, "onactivitycreated: - " + fromdate + " " + todate);               (int = 0; < prod_array.size(); i++) {                 task_name.add(prod_array.get(i).getname());                 log.e("array_val", " " + task_name);                  switch (i) {                     case 0:                         ft = new float[prod_array.get(i).getdata().size()];                         log.e("ft_size", "" + ft.length);                         (int j = 0; j < prod_array.get(i).getdata().size(); j++) {                             ft[j] = float.parsefloat(prod_array.get(i).getdata().get(j));                             log.e("ft_val", "" + ft[j]);                         }                         break;                     case 1:                         tt = new float[prod_array.get(i).getdata().size()];                         log.e("tt_size", "" + tt.length);                         (int j = 0; j < prod_array.get(i).getdata().size(); j++) {                             tt[j] = float.parsefloat(prod_array.get(i).getdata().get(j));                             log.e("tt_val", "" + tt[j]);                         }                         break;                     case 2:                         im = new float[prod_array.get(i).getdata().size()];                         log.e("im_size", "" + im.length);                         (int j = 0; j < prod_array.get(i).getdata().size(); j++) {                             im[j] = float.parsefloat(prod_array.get(i).getdata().get(j));                             log.e("im_val", "" + im[j]);                         }                         break;                     case 3:                         em = new float[prod_array.get(i).getdata().size()];                         log.e("em_size", "" + em.length);                         (int j = 0; j < prod_array.get(i).getdata().size(); j++) {                             em[j] = float.parsefloat(prod_array.get(i).getdata().get(j));                             log.e("em_val", "" + em[j]);                         }                         break;                 }             }         } else {             log.e(tag, "oncreateview: bundle b null");         }     }      private bardata getproductivitybarchartdata(barchart barchart) {          if(ft!=null){             int[] j = new int[ft.length];             //int[] j = new int[f.length];             (int = 0; < j.length; i++) {                 float val1 = (ft[i]);                 //float val1 = (f[i]);                // float val2 = (float) (math.random() * val1) + / 3;                 float val2 = (tt[i]);                 //float val2 = (t[i]);                 //float val3 = (float) (math.random() * val1) + / 3;                 float val3 = (im[i]);                 //float val3 = (im_[i]);                 //float val4 = (float) (math.random() * val1) + / 3;                 float val4 = (em[i]);                 //float val4 = (e[i]);                 log.e("barchartdata ",val1+" "+val2+" "+val3+" "+val4);                 //barentries.add(new barentry(i, new float[]{100, 80, 60, 30}));                 barentries.add(new barentry(i, new float[]{val1, val2, val3, val4}));             }         }          log.e("barentries_size", "" + barentries.size() + " " + barentries.tostring());          bardataset set1;          if (barchart.getdata() != null &&                 barchart.getdata().getdatasetcount() > 0) {             log.e("in_if", "" + barchart.getdata().getdatasetcount());             set1 = (bardataset) barchart.getdata().getdatasetbyindex(0);             set1.setvalues(barentries);             barchart.getdata().notifydatachanged();             barchart.notifydatasetchanged();             barchart.invalidate();         }         else             {             set1 = new bardataset(barentries, "");                 log.e("else-barentries","" + barentries.size() +" " + barentries.tostring());             set1.setcolors(getcolors());             log.e("set", "" + set1.getentrycount());             arraylist<ibardataset> datasets = new arraylist<>();             datasets.add(set1);              bardata data = new bardata(datasets);             data.setdrawvalues(false);          }         bardata d = new bardata(set1);         log.e("test", "" + d.getdatasetcount());         return d;     }      private int[] getcolors() {         int stacksize = 4;         // have many colors stack-values per entry         int[] colors = new int[stacksize];         (int = 0; < colors.length; i++) {             colors[i] = colortemplate.material_colors[i];         }         return colors;     } } 


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -