title: Update Android ListView in Widget
body: <p>I have a problem with my App-Widget when i try to update the content.</p>
  
  <p>The widget looks like this: 
  <a href="http://i.stack.imgur.com/tEGLv.jpg" rel="nofollow">http://i.stack.imgur.com/tEGLv.jpg</a></p>
  
  <p>The Widget consists of a Header (Buttons + TextViews) and a ListView.
  If the User press a Button the ListView shows the content of the next day and everything works fine. But the problem is, if i try to update the widget from my app or the automatically timeperiod the ListView doenst update. </p>
  
  <p>Here the my Code.</p>
  
  <p>AppWidgetProvider:</p>
  
  <pre><code>public class StudyWidgetProvider extends AppWidgetProvider{
  
  private static String BUTTON_BACK = "BUTTON_BACK";
  private static String BUTTON_FORWARD = "BUTTON_FORWARD";
  
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
      context.startService(new Intent(context, UpdateService.class));
  }
  
  @Override
  public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      if(WidgetViewsFactory.currentDate == null){
          WidgetViewsFactory.currentDate = Calendar.getInstance();
      }
  
      String action = intent.getAction();
      if(action != null){
          if(action.equals(BUTTON_BACK)){
              WidgetViewsFactory.currentDate.add(Calendar.DATE, -1);
          }
          if(action.equals(BUTTON_FORWARD)){
              WidgetViewsFactory.currentDate.add(Calendar.DATE, 1);
          }
      }
  
      context.startService(new Intent(context, UpdateService.class));
  }   
   }
  </code></pre>
  
  <p>The UpdateService which handles the update of the Widget. </p>
  
  <pre><code>    @Override
  @Deprecated
  public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      Log.i(LOG_TAG, "Update-Service started");
  
      RemoteViews widget = buildUpdate(this);
      ComponentName thisWidget = new ComponentName(this, StudyWidgetProvider.class);
      AppWidgetManager manager = AppWidgetManager.getInstance(this);
  
      manager.notifyAppWidgetViewDataChanged(manager.getAppWidgetIds(thisWidget), R.id.words);
      manager.updateAppWidget(thisWidget, widget);
  
      Log.i(LOG_TAG, "The Content!");
      if(widgetObjects == null){
          Log.i(LOG_TAG, "WidgetObjects sind NULL");
      } else {
          Log.i(LOG_TAG, "WidgetObjects sind NICHT-NULL");
  
          for(int i=0; i&lt;widgetObjects.size(); i++){
              Log.i(LOG_TAG, "--- " + widgetObjects.get(i).getName());
          }
      }
  
      Log.i(LOG_TAG, "Update-Service ended");
  }
  
  
  public RemoteViews buildUpdate(Context context){
      RemoteViews retVal = new RemoteViews(context.getPackageName(), R.layout.widget);
  
      if(WidgetViewsFactory.currentDate == null){
          WidgetViewsFactory.currentDate = Calendar.getInstance();
      }
  
      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
      ComponentName thisAppWidget = new ComponentName(context.getPackageName(), StudyWidgetProvider.class.getName());
      int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
  
      loadSDCardContent(context, appWidgetManager, appWidgetIds);
  
      //Setzen des Widget-UIs
      Intent intent = new Intent(context, WidgetService.class);
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds);
      intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
  
      retVal.setRemoteAdapter(R.id.words, intent);
  
      SimpleDateFormat dateFormater = new SimpleDateFormat("EEEE");
      retVal.setTextViewText(R.id.widget_day, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
      dateFormater = new SimpleDateFormat("dd.MM.yyyy");
      retVal.setTextViewText(R.id.widget_date, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
  
      //Button-Actions
      Intent backIntent = new Intent(context, StudyWidgetProvider.class);
      backIntent.setAction(BUTTON_BACK);
      Intent forwardIntent = new Intent(context, StudyWidgetProvider.class);
      forwardIntent.setAction(BUTTON_FORWARD);
      PendingIntent backPendingIntent = PendingIntent.getBroadcast(context, 0, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      PendingIntent forwardPendingIntent = PendingIntent.getBroadcast(context, 0, forwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      retVal.setOnClickPendingIntent(R.id.widget_but_back, backPendingIntent);
      retVal.setOnClickPendingIntent(R.id.widget_but_forward, forwardPendingIntent);
  
      Intent clickIntent = new Intent(context, WidgetActivity.class);
      PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      retVal.setPendingIntentTemplate(R.id.words, clickPI);
  
      return retVal;
  }
  </code></pre>
  
  <p>And heres how i update the widget from my app:</p>
  
  <pre><code>public RemoteViews buildUpdate(Context context){
      RemoteViews retVal = new RemoteViews(context.getPackageName(), R.layout.widget);
  
      if(WidgetViewsFactory.currentDate == null){
          WidgetViewsFactory.currentDate = Calendar.getInstance();
      }
  
      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
      ComponentName thisAppWidget = new ComponentName(context.getPackageName(), StudyWidgetProvider.class.getName());
      int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
  
      loadSDCardContent(context, appWidgetManager, appWidgetIds);
  
      //Setzen des Widget-UIs
      Intent intent = new Intent(context, WidgetService.class);
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds);
      intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
  
      retVal.setRemoteAdapter(R.id.words, intent);
  
      SimpleDateFormat dateFormater = new SimpleDateFormat("EEEE");
      retVal.setTextViewText(R.id.widget_day, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
      dateFormater = new SimpleDateFormat("dd.MM.yyyy");
      retVal.setTextViewText(R.id.widget_date, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
  
      //Button-Actions
      Intent backIntent = new Intent(context, StudyWidgetProvider.class);
      backIntent.setAction(BUTTON_BACK);
      Intent forwardIntent = new Intent(context, StudyWidgetProvider.class);
      forwardIntent.setAction(BUTTON_FORWARD);
      PendingIntent backPendingIntent = PendingIntent.getBroadcast(context, 0, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      PendingIntent forwardPendingIntent = PendingIntent.getBroadcast(context, 0, forwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      retVal.setOnClickPendingIntent(R.id.widget_but_back, backPendingIntent);
      retVal.setOnClickPendingIntent(R.id.widget_but_forward, forwardPendingIntent);
  
      Intent clickIntent = new Intent(context, WidgetActivity.class);
      PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      retVal.setPendingIntentTemplate(R.id.words, clickPI);
  
      return retVal;
  }
  </code></pre>
  
  <p>The strange thing is when i hit the button of the widget, everythings work as i wish. The onDatasetChanged-method of my listview gets called and the content is changing, but if i try this from my app nothing happens</p>
  
  <p>I hope someone of you have an idea why this isnt working. 
  Im stuck on this for now 2 days and i have no clue...</p>
  
  <p>Thanks for helping!</p>
  
  <p><strong>EDIT</strong>
  Ive found out that this isnt working on my device (htc with miui v4) but on the emulator.</p>
  
  <p><strong>EDIT 2</strong>
  I installed Android 4 (with Sense 4) on my device, and after that it worked too. It looks like MIUI was the problem.</p>