我已经编写了一个AppWidget,它显示ContentProvider在ListView中的一些数据,但是我无法更新它。当我第一次创建小部件时,它会正确填充,但是在AlarmManager的PendingIntent到达后,ListView上不会发生更新。下面的代码:android appwidget listview不更新
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);
奇怪的是,当更新发生时,的onUpdate()方法运行 - 我看到从Log.d调用的输出 - 但目前还没有onDataSetChanged()的调用中我的RemoteViewsFactory。即使我调用notifyAppWidgetViewDataChanged,也没有任何效果。
谢谢!这对我有效。 – buntwoi
这种技术可以工作,但随着我的内存配置文件的部件,内存消耗增长。为了调试/分析目的,我每隔一分钟设置一次警报。 –
这个问题让我受不了。你拯救了我的一天。你是如何遇到这个解决方案的?只是好奇。谢谢Prateek。 –