android - Update issue of an app widget using collection -


i have created app widget using collection app, widget shows date , list of item on particular date. works fine , widget updating required, happen while changing date in widget clicking next , previous button, list not refresh means items not updated on particular date. behaviour random , occur only. why happen, wrong in code.

code have use is:

widgetprovider.class

public class widgetprovider extends appwidgetprovider  {        private thememanager    m_thememanagerobject;      private static string   widget_next_button = "in.test.widgetapp.widget_next_button";      private static string   widget_prev_button = "in.test.widgetapp.widget_prev_button";          @override     public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids)      {                        super.onupdate(context, appwidgetmanager, appwidgetids);          // set date current date         notemanager.getsingletonobject().setwidgettocurrentdate();          // code update widget current date          updateappwidget(context, appwidgetmanager.getinstance(context), appwidgetids);     }         @override     public void onreceive(context context, intent intent)      {                    super.onreceive(context, intent);          int numofdays = 1;          componentname thiswidget = new componentname(context, widgetprovider.class);         appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(context);         int[] appwidgetids = appwidgetmanager.getappwidgetids(thiswidget);          if (intent.getaction().equals(widget_next_button))          {             // increase no of days 1             // update widget new date              notemanager.getsingletonobject().setwidgetdate(numofdays);                       updateappwidget(context, appwidgetmanager.getinstance(context), appwidgetids);         }            else if (intent.getaction().equals(widget_prev_button))          {             // decrease no of days 1             // update widget new date              notemanager.getsingletonobject().setwidgetdate(-numofdays);                      updateappwidget(context, appwidgetmanager.getinstance(context), appwidgetids);         }                        }          public void updateappwidget(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) {     // folder path of all-page-view     contextwrapper cw = new contextwrapper(context.getapplicationcontext());     file customdirectorypath = cw.getdir(utilities.custom_directory_name_prefix, context.mode_private);     file allpagedirectorypath = new file(customdirectorypath.getpath() + "/" + utilities.all_page_directory_name_prefix);       if (!(allpagedirectorypath.exists()))         allpagedirectorypath.mkdirs();      // create singleton object of thememanager class     m_thememanagerobject = thememanager.getsingletonobject();     m_thememanagerobject.readtheme(allpagedirectorypath.getpath());      // create instance of simpledateformat class     simpledateformat dateformater = new simpledateformat("dd-mmm, eee", locale.us);      /* loop through widget instances */     (int widgetid : appwidgetids)      {          // create instance of remote view class         remoteviews remoteview = new remoteviews(context.getpackagename(), r.layout.widget_list);                intent svcintent = new intent(context, widgetservice.class);         svcintent.setdata(uri.fromparts("content", string.valueof(widgetid), null));                 remoteview.setremoteadapter(r.id.widget_list, svcintent);             // show day, month , week day inside widget         remoteview.settextviewtext(r.id.txt_date, dateformater.format(notemanager.getsingletonobject().getwidgetdate().gettime()));          // if list empty. show empty widget juswrite-icon & empty text user                   remoteview.setemptyview(r.id.widget_list, r.id.widget_empty_text);                        // on click of next button         intent nextbuttonintent = new intent(widget_next_button);         /* use widgetid second parameter - helped me better address particular widget instance */         pendingintent nextbuttonpendingintent = pendingintent.getbroadcast(context, widgetid, nextbuttonintent, pendingintent.flag_update_current);         remoteview.setonclickpendingintent(r.id.btn_next_month, nextbuttonpendingintent);         remoteview.setint(r.id.btn_next_month, "setbackgroundresource", m_thememanagerobject.getnextbuttonbgimage());          // on click of previous button         intent prevbuttonintent = new intent(widget_prev_button);         /* use widgetid second parameter - same above */         pendingintent prevbuttonpendingintent = pendingintent.getbroadcast(context, widgetid, prevbuttonintent, pendingintent.flag_update_current);         remoteview.setonclickpendingintent(r.id.btn_prev_month, prevbuttonpendingintent);         remoteview.setint(r.id.btn_prev_month, "setbackgroundresource", m_thememanagerobject.getprevbuttonbgimage());          // open application on click of app widget         intent clickintent = new intent(context, allpageviewactivity.class);         pendingintent clickpi = pendingintent.getactivity(context, 0,clickintent,pendingintent.flag_update_current);         remoteview.setonclickpendingintent(r.id.widget_empty_text, clickpi);         remoteview.setonclickpendingintent(r.id.txt_date, clickpi);          /* update 1 widget instance @ time*/         appwidgetmanager.updateappwidget(widgetid, remoteview);     } } } 

widgetservice.class

public class widgetservice extends remoteviewsservice  {     @override     public remoteviewsfactory ongetviewfactory(intent intent)      {         return(new widgetdisplay(this.getapplicationcontext(), intent));     } } 

widgetdisplay.class

public class widgetdisplay implements remoteviewsservice.remoteviewsfactory {     private file m_customdirectorypath, m_allpagedirectorypath;      private notemanager m_notemanagerobject;      private thememanager m_thememanagerobject;      private arraylist<string>   m_alarmitemnamearraylist;      private arraylist<integer>  m_itemindexarray;      private context ctxt=null;      int appwidgetid;      bitmap canvasbackground;      public widgetdisplay(context ctxt, intent intent)      {         this.ctxt=ctxt;          appwidgetid = intent.getintextra(appwidgetmanager.extra_appwidget_id,                 appwidgetmanager.invalid_appwidget_id);          setimageinview(this.ctxt);      }      private void setimageinview(context context)      {         contextwrapper cw = new contextwrapper(ctxt.getapplicationcontext());         m_customdirectorypath = cw.getdir(utilities.custom_directory_name_prefix, context.mode_private);         m_allpagedirectorypath = new file(m_customdirectorypath.getpath() + "/" + utilities.all_page_directory_name_prefix);           m_notemanagerobject = notemanager.getsingletonobject();         m_thememanagerobject = thememanager.getsingletonobject();          m_notemanagerobject.readsettings(m_allpagedirectorypath.getpath());         m_notemanagerobject.readallpagechangesfromfile(m_allpagedirectorypath.getpath());         m_notemanagerobject.readalarmfromfile(m_allpagedirectorypath.getpath());         m_thememanagerobject.readtheme(m_allpagedirectorypath.getpath());          m_alarmitemnamearraylist = new arraylist<string>(m_notemanagerobject.getalarmcount());         m_itemindexarray = new arraylist<integer>(m_notemanagerobject.getalarmcount());          simpledateformat sdformatter = new simpledateformat("dd-mmm-yyyy", locale.us);               string selecteddate = sdformatter.format(m_notemanagerobject.getwidgetdate());          for(int i=0; i<m_notemanagerobject.getalarmcount(); i++)         {             string arraydate = sdformatter.format(m_notemanagerobject.getalarmtime(i));                      if(selecteddate.equals(arraydate))             {                 file notedirectorypath = new file(m_customdirectorypath.getpath() + "/" + m_notemanagerobject.getalarmfolder(i));                 m_alarmitemnamearraylist.add(notedirectorypath.getpath() + "/" + m_notemanagerobject.getalarmitem(i));                  m_itemindexarray.add(i);             }         }     }      @override     public int getcount()      {         return(m_alarmitemnamearraylist.size());     }      @override     public remoteviews getviewat(int position)      {                new imageloadertask(position).executeonexecutor(asynctask.thread_pool_executor);          // set combine image image view using remote view instance         remoteviews remoteview = new remoteviews(ctxt.getpackagename(), r.layout.widget_list_item);         remoteview.setimageviewbitmap(r.id.image_view, canvasbackground);          // set time text view using remote view instance         simpledateformat timeformater;          if(m_notemanagerobject.get24hourformat())         {             timeformater = new simpledateformat("hh:mm", locale.us);         }         else         {             timeformater = new simpledateformat("hh:mm a", locale.us );         }          // show time on top of each image view         string time = timeformater.format(m_notemanagerobject.getalarmtime(m_itemindexarray.get(position)));                     remoteview.settextviewtext(r.id.text_alarm_time,  time);                  intent clickintent = new intent(ctxt, allpageviewactivity.class);         pendingintent clickpi=pendingintent.getactivity(ctxt, 0,clickintent,pendingintent.flag_update_current);         remoteview.setonclickpendingintent(r.id.image_view, clickpi);          return(remoteview);     }      class imageloadertask extends asynctask<url, integer, long>     {         private int position;          imageloadertask(int position)         {             this.position = position;         }          @override         protected void onpreexecute()         {             // foreground , background image             bitmap bitmapimage = bitmapfactory.decodefile(m_alarmitemnamearraylist.get(position)).copy(bitmap.config.argb_8888, true);             canvasbackground = bitmapfactory.decoderesource(ctxt.getresources(), m_thememanagerobject.getwidgetlistitembgimage(m_itemindexarray.get(position), bitmapimage)).copy(bitmap.config.argb_8888, true);              // scaled foreground image , combine background image             bitmapimage = bitmap.createscaledbitmap(bitmapimage, 380, bitmapimage.getheight() / 2, true);                            canvas comboimage = new canvas(canvasbackground);             comboimage.drawbitmap(bitmapimage, 0f, 0f, null);         }          @override         protected long doinbackground(url... urls)         {             return null;         }          @override         protected void onprogressupdate(integer... progress)         {          }          @override         protected void onpostexecute(long result)         {          }     }      @override     public void oncreate(){      }      @override     public void ondestroy(){     }      @override     public remoteviews getloadingview()     {         return(null);     }      @override     public int getviewtypecount(){         return(1);     }      @override     public long getitemid(int position){         return(position);     }      @override     public boolean hasstableids(){         return(true);     }      @override     public void ondatasetchanged(){     } } 

widgetprovider.class

public class widgetprovider extends appwidgetprovider  {        private notemanager         m_notemanagerobject;      private thememanager        m_thememanagerobject;      private simpledateformat    m_dateformater;      private static string       widget_next_button = "in.test.widgetapp.widget_next_button";      private static string       widget_prev_button = "in.test.widgetapp.widget_prev_button";          public widgetprovider()     {         // create singleton object of notemanager class         m_notemanagerobject = notemanager.getsingletonobject();         // create singleton object of thememanager class         m_thememanagerobject = thememanager.getsingletonobject();         // create instance of simpledateformat class         m_dateformater = new simpledateformat("dd-mmm, eee", locale.us);     }      @override     public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids)      {                        super.onupdate(context, appwidgetmanager, appwidgetids);          // set date current date         m_notemanagerobject.setwidgettocurrentdate();          // folder path of all-page-view         contextwrapper cw = new contextwrapper(context.getapplicationcontext());         file customdirectorypath = cw.getdir(utilities.custom_directory_name_prefix, context.mode_private);         file allpagedirectorypath = new file(customdirectorypath.getpath() + "/" + utilities.all_page_directory_name_prefix);           if (!(allpagedirectorypath.exists()))             allpagedirectorypath.mkdirs();          m_thememanagerobject.readtheme(allpagedirectorypath.getpath());               // set intent starts widgetservice,         // provide views collection.            // when intents compared, extras ignored, need embed extras         // data extras not ignored.         intent intent  = new intent(context, widgetservice.class);         intent.setdata(uri.fromparts("content", string.valueof(appwidgetids), null));          // instantiate remoteviews object app widget layout.         // set remoteviews object use remoteviews adapter.          // adapter connects remoteviewsservice  through specified intent.         // how populate data.         remoteviews remoteview = new remoteviews(context.getpackagename(), r.layout.widget_list);                    remoteview.setremoteadapter(r.id.widget_list, intent);            // show day, month , week day inside widget         remoteview.settextviewtext(r.id.txt_date, m_dateformater.format(m_notemanagerobject.getwidgetdate().gettime()));          // empty view displayed when collection has no items.                 remoteview.setemptyview(r.id.widget_list, r.id.widget_empty_text);                        // on click of next button         // section makes possible items have individualized behavior.         // set action intent.         // when user touches particular view, have effect of         // broadcasting action.         intent nextbuttonintent = new intent(context, widgetprovider.class);                     nextbuttonintent.setaction(widget_next_button);         pendingintent nextbuttonpendingintent = pendingintent.getbroadcast(context, 0, nextbuttonintent, pendingintent.flag_update_current);         remoteview.setonclickpendingintent(r.id.btn_next_month, nextbuttonpendingintent);         remoteview.setint(r.id.btn_next_month, "setbackgroundresource", m_thememanagerobject.getnextbuttonbgimage());          // on click of previous button         // section makes possible items have individualized behavior.         // set action intent.         // when user touches particular view, have effect of         // broadcasting action.         intent prevbuttonintent = new intent(context, widgetprovider.class);         prevbuttonintent.setaction(widget_prev_button);         pendingintent prevbuttonpendingintent = pendingintent.getbroadcast(context, 0, prevbuttonintent, pendingintent.flag_update_current);         remoteview.setonclickpendingintent(r.id.btn_prev_month, prevbuttonpendingintent);         remoteview.setint(r.id.btn_prev_month, "setbackgroundresource", m_thememanagerobject.getprevbuttonbgimage());          // open application on click of app widget         intent clickintent = new intent(context, allpageviewactivity.class);         pendingintent clickpi = pendingintent.getactivity(context, 0,clickintent,pendingintent.flag_update_current);         remoteview.setonclickpendingintent(r.id.widget_empty_text, clickpi);         remoteview.setonclickpendingintent(r.id.txt_date, clickpi);           appwidgetmanager.updateappwidget(appwidgetids, remoteview);      }         @override     public void onreceive(context context, intent intent)      {                    super.onreceive(context, intent);          int numofdays = 1;          componentname thiswidget = new componentname(context, widgetprovider.class);         appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(context);         int[] appwidgetids = appwidgetmanager.getappwidgetids(thiswidget);          if (intent.getaction().equals(widget_next_button))          {             // increase no of days 1             m_notemanagerobject.setwidgetdate(numofdays);              // update remote view             remoteviews remoteview = new remoteviews(context.getpackagename(), r.layout.widget_list);                            remoteview.settextviewtext(r.id.txt_date, m_dateformater.format(m_notemanagerobject.getwidgetdate().gettime()));             appwidgetmanager.updateappwidget(appwidgetids, remoteview);               // update list content of widget            // call ondatasetchanged() method of widgetdisplay class             appwidgetmanager.notifyappwidgetviewdatachanged(appwidgetids, r.id.widget_list);         }            else if (intent.getaction().equals(widget_prev_button))          {             // decrease no of days 1             m_notemanagerobject.setwidgetdate(-numofdays);                // update remote view             remoteviews remoteview = new remoteviews(context.getpackagename(), r.layout.widget_list);                            remoteview.settextviewtext(r.id.txt_date, m_dateformater.format(m_notemanagerobject.getwidgetdate().gettime()));             appwidgetmanager.updateappwidget(appwidgetids, remoteview);               // update list content of widget               // call ondatasetchanged() method of widgetdisplay class             appwidgetmanager.notifyappwidgetviewdatachanged(appwidgetids, r.id.widget_list);         }                        } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -