Android appwidget listview not updating

I wrote an AppWidget that displays some data in a ListView from a ContentProvider, but I have problems updating it. When I first create a widget, it fills correctly, but after receiving the AlarmManager PendingIntent request in the ListView, no updates occur. Here is the code:

Intent update = new Intent(context, MenuWidgetProvider.class); update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); PendingIntent pIUpdate = PendingIntent.getBroadcast(context, 0, update, 0); ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)). set(AlarmManager.RTC, nextTime.toMillis(false), pIUpdate); Log.d(TAG, "updating: " + dmt.mealName); for (int i = 0; i < appWidgetIds.length; ++i) { int widgetId = appWidgetIds[i]; RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_menu); // meal name views.setTextViewText(R.id.widget_locname, prefs.getString(PREF_WIDGET_LOCNAME, "")); // adapter Intent adapter = new Intent(context, MenuWidgetAdapterService.class); adapter.putExtra(EXTRA_LOCATIONID, prefs.getInt(PREF_WIDGET_LOCID, -1)); adapter.putExtra(EXTRA_MEALNAME, dmt.mealName); adapter.putExtra(EXTRA_DATE, dmt.date.toString()); views.setRemoteAdapter(R.id.widget_list, adapter); // update manager.updateAppWidget(widgetId, views); } super.onUpdate(context, manager, appWidgetIds); 

It's strange when the update happens, the onUpdate () method starts - I see the output from the Log.d call, but in my RemoteViewsFactory there is no call to onDataSetChanged (). Even when I call notifyAppWidgetViewDataChanged, there is no effect.

+7
source share
3 answers

This is because your RemoteViewsFactory caches the factory that it creates for each widget id. Therefore, when the same widget sends a request to create a new factory (through your widget adapter service), the same factory is returned. Therefore, the list view is not "updated".

How I worked on this:

In your WidgetProviderClass

 adapter.setData(Uri.fromParts("content", String.valueOf(appWidgetIds[i]+randomNumber), null)); 

where randomNumber is a random number (the public static member of your WidgetProvider class).

Then in your RemoteViewsFactory class do:

 appWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart()) - WidgetProvider.randomNumber; 

This "tricks" the Service into thinking that it needs to create a factory for another widget and, therefore, creates a new factory with updated values ​​that are bound to your widget.

Hope this helps.

+12
source

Even I had the same problem. The remote view of the widget has this problem.

Solution: Send the broadcast to yourself and update the list. ie Set the data to full view, then send the broadcast to yopurself.

Hope this helps

0
source

Try a dynamic number

In your WidgetProviderClass:

 int randomNumber=(int)(Math.random()*1000); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId+randomNumber); intent.setData(Uri.fromParts("content", String.valueOf(appWidgetId), null)); intent.putExtra("random",randomNumber ); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

And in your RemoteViewsFactory class do:

 AppWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart()); 
0
source

All Articles