android appwidget listview没有更新 [英] android appwidget listview not updating

查看:57
本文介绍了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没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆