1

I'm creating a Widget with ListView on it. Displaying the result from the Web Service in the ListView is okay. I created a class which is my WidgetService that extends to RemoteViewsService and on RemoteViewsFactory I call my ListViewProvider that has the Web Service. I added a refresh button that when clicked it will call the WidgetService again to created the list view.

Here is my code in my WidgetProvider

public class WidgetProvider extends AppWidgetProvider {

   @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
 final int N = appWidgetIds.length;
  for (int i = 0; i < N; ++i) {
        RemoteViews remoteViews = updateWidgetListView(context,
                appWidgetIds[i]);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);

//ListView
    Intent svcIntent = new Intent(context, WidgetService.class);
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget, svcIntent);
    remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);

    //REFRESH
    PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, svcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.ivRefreshWidget, updatePendingIntent);

 return remoteViews;
}

As you can see in the refresh part I tried using PendingIntent to call the WidgetService but when testing it doesn't refresh the list. Thank you for the help in advance.

natsumiyu
  • 3,217
  • 7
  • 30
  • 54
  • I already answered it previously - http://stackoverflow.com/a/12907825/726863 Also, here as well - http://stackoverflow.com/a/36005649/726863 – Lalit Poptani Sep 08 '16 at 10:38
  • @LalitPoptani based on the link I should use a `Service` instead of using `RemoteViewsService` for my `WidgetService` ? – natsumiyu Sep 09 '16 at 00:24
  • Also @LalitPoptani I'm using `notifyAppWidgetViewDataChanged` on my `WidgetProvider` should I put it somewhere else? I wanted to trigger the refresh when button is clicked. – natsumiyu Sep 09 '16 at 00:34

2 Answers2

1

In my onUpdateI set an Action to be called in my onReceive, and also put the appWidgetIds to be used as well in the onReceive

Intent refreshIntent = new Intent(context, WidgetProvider.class);
        refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

then in onReceive I call the onUpdate

if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            if (appWidgetIds != null && appWidgetIds.length > 0) {
                Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
                this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
            }
        }
    }
natsumiyu
  • 3,217
  • 7
  • 30
  • 54
  • So the first code will go for the onclickpendingintent for the button? Well that didn't work. I have a refresh button in the widget that I would like to use to refresh the widget itself and call `onUpdate`. Any idea? – Si8 Nov 23 '16 at 12:05
  • @Si8 first you need to set the button in the `onUpdate`and then set an action, for me it's `AppWidgetManager.ACTION_APPWIDGET_UPDATE` then get the action in `onReceive` where you will call the `this.onUpdate()` – natsumiyu Nov 24 '16 at 08:17
  • http://stackoverflow.com/questions/40776920/how-to-manually-call-onupdate-from-within-the-widget-itself If you would like to take a look... – Si8 Nov 24 '16 at 09:03
  • @Si8 i post an answer to your question check it out thanks – natsumiyu Nov 25 '16 at 09:31
0
arrayList = new ArrayList<AllPostsProvider>();
arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());

and when click refreash button

 arrayList.clear();
 arrayList = new ArrayList<AllPostsProvider>();
 arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());
snehasish
  • 171
  • 2
  • 10