且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Listivew 小部件不会自动更新

更新时间:2023-10-03 17:53:58

建议你试试下面粘贴的代码:

I suggest you to try the code pasted below:

ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listWidget);
appWidgetManager.updateAppWidget(component, remoteView);

notifyAppWidgetViewDataChanged() --->通知所有指定的AppWidget 实例中的指定collection view 使其当前数据无效.

notifyAppWidgetViewDataChanged() --->Notifies the specified collection view in all the specified AppWidget instances to invalidate their currently data.

因此,当您调用 notifyAppWidgetViewDataChanged() 时,将调用 RemoteViewsFactoryonDataSetChanged() 方法,该方法用作您的适配器列表视图.您可以在 onDataSetChanged() 中进行 web 服务/网络相关操作、从数据库中获取数据操作和其他操作,获取响应并将其添加到您的数据集中,可能是 ArrayList 或任何此类 Collection.

So when you call notifyAppWidgetViewDataChanged() then the onDataSetChanged() method of RemoteViewsFactory will be called which is serving as a Adapter for your ListView. You can do web-service/network related operations, data fetching operation from database and other operations in onDataSetChanged(), get the response and add it to your data-set which maybe ArrayList or any such Collection.

您可以参考Using App Widgets with Collections