android appwidget listview not updating(android appwidget listview没有更新)
问题描述
我编写了一个 AppWidget,它在 ListView 中显示来自 ContentProvider 的一些数据,但我无法更新它.当我第一次创建小部件时,它会正确填充,但在 AlarmManager 的 PendingIntent 到达后,ListView 上不会发生更新.代码如下:
I've written an AppWidget that displays some data in a ListView from a ContentProvider, but I have trouble having it update. When I first create the widget it gets populated correctly, but after the AlarmManager's PendingIntent arrives, no updates occurs on the ListView. Here's 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);
奇怪的是,当更新发生时,onUpdate() 方法运行——我看到了 Log.d 调用的输出——但是在我的 RemoteViewsFactory 中没有调用 onDataSetChanged().即使我调用 notifyAppWidgetViewDataChanged,也没有效果.
Weird thing is, when the update happens, the onUpdate() method runs--I see the output from the Log.d call--but there is no call to onDataSetChanged() in my RemoteViewsFactory. Even when I call notifyAppWidgetViewDataChanged, no effect.
推荐答案
这是因为您的 RemoteViewsFactory 缓存了它为每个小部件 ID 创建的工厂.因此,当同一个小部件发送创建新工厂的请求(通过您的小部件适配器服务)时,将返回同一个工厂.因此,列表视图没有更新".
This is because your RemoteViewsFactory caches the factory it creates for each widget ID. Hence when the same widget sends a request to create a new factory (through your Widget Adapter Service), the same factory is returned. Hence, the list view is not "updated".
我是如何解决这个问题的:
How I worked around it:
在您的 WidgetProviderClass 中
In your WidgetProviderClass
adapter.setData(Uri.fromParts("content", String.valueOf(appWidgetIds[i]+randomNumber), null));
其中 randomNumber 是一个随机数(WidgetProvider 类的公共静态成员).
where randomNumber is a random number (a public Static member of your WidgetProvider class).
然后在你的 RemoteViewsFactory 类中,做:
Then in your RemoteViewsFactory class, do:
appWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart())
- WidgetProvider.randomNumber;
这愚弄"了服务,使其认为它必须为另一个小部件创建一个工厂,因此,使用更新的值创建一个新工厂,并将其附加到您的小部件.
This "fools" the Service into thinking that it has to create a Factory for another widget, and therefore, creates a new factory with the updated values, which is attached to your widget.
希望这会有所帮助.
这篇关于android appwidget listview没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:android appwidget listview没有更新
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01