android appwidget listview not updating

守給你的承諾、 提交于 2019-12-03 07:12:28

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:

In your WidgetProviderClass

adapter.setData(Uri.fromParts("content", String.valueOf(appWidgetIds[i]+randomNumber), null));

where randomNumber is a random number (a public Static member of your WidgetProvider class).

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.

Hope this helps.

Even I had same problem. RemoteView of widget has this problem.

Solution: Send broadcast to yourself and refresh list again. i.e Set data to your complete view and then send broadcast to yopurself.

Hope this helps

Try dynamic number

In your WidgetProviderClass:

int randomNumber=(int)(Math.random()*1000); 
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId+randomNumber);

           intent.setData(Uri.fromParts("content", String.valueOf(appWidgetId), null));

           intent.putExtra("random",randomNumber );
           intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
           rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

And in your RemoteViewsFactory class, do :

AppWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!