
我的朋友已完成一个应用程序,现在他为他的应用程序创建了一个小部件,可用于以列表视图方式显示任务列表.首先,当他拖动窗口小部件并安装在主屏幕时,它将正常工作,并以列表视图方式显示任务.
他的问题是,当他对他的应用程序进行更改然后回到他的主屏幕时,没有反映出任何变化.他正在使用onReceive()方法来更新小部件
import androID.app.PendingIntent;import androID.appWidget.AppWidgetManager;import androID.appWidget.appwidgetprovider;import androID.content.Componentname;import androID.content.Context;import androID.content.Intent;import androID.content.SharedPreferences;import androID.content.SharedPreferences.Editor;import androID.net.Uri;import androID.util.Log;import androID.Widget.RemoteVIEws;public class WidgetTaskSchedular extends appwidgetprovider { private final String TAG = "CalendarVIEwSample:" + this.getClass().getname(); SharedPreferences sharedprefer; @OverrIDe public voID onReceive(Context context, Intent intent) { super.onReceive(context, intent); if(intent.getAction().equals("update_Widget")) { int[] appID=new int[1]; sharedprefer=context.getSharedPreferences("TaskWidget", 0); appID[0]=sharedprefer.getInt("Widget_key",0); Log.i(TAG,"AppWidgetID"+appID[0]); onUpdate(context, AppWidgetManager.getInstance(context),appID); } } public static String EXTRA_WORD= "com.capsone.testing.calendar.WORD"; @SuppressWarnings("deprecation") @OverrIDe public voID onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIDs) { super.onUpdate(context, appWidgetManager, appWidgetIDs); for (int i=0; i<appWidgetIDs.length; i++) { sharedprefer=context.getSharedPreferences("TaskWidget", 0); SharedPreferences.Editor editor=sharedprefer.edit(); editor.putInt("Widget_key", appWidgetIDs[i]); editor.commit(); int a= sharedprefer.getInt("Widget_key", 0); Log.i(TAG,"SharedpreferenceWidgetID:"+a); //ID=appWidgetIDs[i]; Log.i(TAG,"LengthofWidget:"+appWidgetIDs.length); // Log.i(TAG,"TestAppWidget:"+ID); Intent intentWidgetService=new Intent(context, WidgetService.class); intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetIDs[i]); intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME))); RemoteVIEws remoteVIEw=new RemoteVIEws(context.getPackagename(), R.layout.Widgetlayout); remoteVIEw.setRemoteAdapter(appWidgetIDs[i], R.ID.ListWidget, intentWidgetService); Intent clickIntent=new Intent(context, ActionBaractivity.class); PendingIntent clickPendingIntent=PendingIntent .getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteVIEw.setPendingIntentTemplate(R.ID.ListWidget, clickPendingIntent); Componentname component=new Componentname(context,WidgetTaskSchedular.class); appWidgetManager.updateAppWidget(component, remoteVIEw); } }}解决方法:
我建议你试试下面粘贴的代码:
Componentname component=new Componentname(context,WidgetTaskSchedular.class);appWidgetManager.notifyAppWidgetVIEwDataChanged(appWidgetIDs, R.ID.ListWidget);appWidgetManager.updateAppWidget(component, remoteVIEw);notifyAppWidgetVIEwDataChanged()—>通知所有指定的AppWidget实例中的指定集合视图,使其当前数据无效.
因此,当您调用notifyAppWidgetVIEwDataChanged()时,将调用RemoteVIEwsFactory的onDataSetChanged()方法,该方法用作ListVIEw的适配器.您可以执行Web服务/网络相关 *** 作,数据获取 *** 作以及onDataSetChanged()中的其他 *** 作,获取响应并将其添加到您的数据集(可能是ArrayList或任何此类集合).
你可以参考Using App Widgets with Collections
总结以上是内存溢出为你收集整理的android – Listivew Widget不会自动更新全部内容,希望文章能够帮你解决android – Listivew Widget不会自动更新所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)