onGetViewFactory只调用一次为多个部件多个、部件、onGetViewFactory

由网友(雲)分享简介:我有一个appwidget它使用的ListView 。我创建了一个扩展的类 RemoteViewsService :I have an appwidget which uses ListView. I have created a class that extends RemoteViewsService:pu...

我有一个appwidget它使用的ListView 。 我创建了一个扩展的类 RemoteViewsService

I have an appwidget which uses ListView. I have created a class that extends RemoteViewsService:

public class AppWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return (new AppWidgetListFactory(this.getApplicationContext(), intent));
    }
}

在我的 AppWidgetProvider ,我呼吁部件的每个实例下面的方法(对于每个 appWidgetId ):

In my AppWidgetProvider, I call the following method for each instance of the widget (for each appWidgetId):

private static void fillInList(RemoteViews widget, Context context, int appWidgetId) {
    Intent intent = new Intent(context, AppWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    widget.setRemoteAdapter(R.id.appwidget_listview, intent);
}

的构造函数的 AppWidgetListFactory

public AppWidgetListFactory(Context context, Intent intent) {
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    System.out.println("New TVAPWLF for: " + appWidgetId);
    trains = WidgetData.getInstance().widgetMap.get(appWidgetId);
}

现在的问题是,是, fillInList 方法被调用,就像经常有小部件的实例,但 onGetViewFactory 方法只被调用一次。这将导致所有示出的相同的数据的窗口小部件。

Now the problem is, that the fillInList method gets called just as often as there are instances of the widget, but the onGetViewFactory method only gets called once. This results in all the widgets showing the same data.

我该如何解决这个问题?

How can I fix this?

推荐答案

我没有测试此我自己,但看着源头$ C $下 RemoteViewsService.onBind(),我相信你不能改变你的意图的演员,以便它检测到您的 onGetViewFactory()方法,新的呼叫是必要的,因为它使用在 Intent.filterEquals()方法的比较:

I haven't tested this myself, but looking at the source code for RemoteViewsService.onBind(), I believe you can not just vary the extras in your Intent in order for it to detect that a new call to your onGetViewFactory() method is needed since it uses the Intent.filterEquals() method for the comparison:

确定是否两个目的都是相同的意图分辨率(滤波)的目的。也就是说,如果它们的作用,数据,类型,类别和类别是相同的。这不比较列入意图任何额外的数据。

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

我会建议通过您所需要的信息(部件编号?)通过意向的数据,而不是,也许是这样的:

I would suggest passing the information that you need (widget id?) through the Intent's data instead, maybe something like:

private static void fillInList(RemoteViews widget, Context context, int appWidgetId) {
    Intent intent = new Intent(context, AppWidgetService.class);
    intent.setData(Uri.fromParts("content", String.valueOf(appWidgetId), null));
    widget.setRemoteAdapter(R.id.appwidget_listview, intent);
}

和分别在接收侧

public TreinVerkeerAppWidgetListFactory(Context context, Intent intent) {
    this.context = context;
    appWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart());

    System.out.println("New TVAPWLF for: " + appWidgetId);
    trains = WidgetData.getInstance().widgetMap.get(appWidgetId);
}
阅读全文

相关推荐

最新文章