4

List remote view is not updating (I mean refreshing itself) when I am updating the widget from some activity. It comes up till onUpdate of app widget (log showing up). but does not enter into adaptor of list view to inflate it with fresh data .

public void onUpdate(Context context, AppWidgetManager appWidgetManager,

int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        Log.e("called", "onupdate");

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget_lay);

        views.setTextViewText(R.id.textView1,
                "Floating Text will be displayed here .. Daily Thoughts");

        Intent intent = new Intent(context, RemoteServiceEmin.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        views.setRemoteAdapter(R.id.gridview, intent);

        // inflating list remote view

        Intent intentlist = new Intent(context, ListInflater.class);
        intentlist.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        views.setRemoteAdapter(R.id.listview, intentlist);

        // setting onclick on gridview
        Intent itemClickIntent = new Intent(context, MyWidgetProvider.class);
        itemClickIntent.setAction(ITEM_CLICK_ACTION);
        itemClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds[i]);
        PendingIntent itemClickPendingIntent = PendingIntent.getBroadcast(
                context, 0, itemClickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.gridview,
                itemClickPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);

}
madhan kumar
  • 1,560
  • 2
  • 26
  • 36
user2660060
  • 151
  • 1
  • 8

1 Answers1

10

So Finally its done and again all thanks to developer.android.com . All you need to do is , Trigger a broadcast intent from where you you want to handle "update " .

Intent initialUpdateIntent = new Intent(
                AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        initialUpdateIntent
                .setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        sendBroadcast(initialUpdateIntent);

and then catch it in onRecieve of Appwidget , and update the Remoteview using

 notifyAppWidgetViewDataChanged(). 

Something like the codesnippetbelow

 else if (intent.getAction().equals(
            AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {

         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            ComponentName thisAppWidget = new ComponentName(context.getPackageName(), MyWidgetProvider.class.getName());
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
             appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,  R.id.listviewnew);
           Log.e("finally after a whole day", "working :");  

    }
user2660060
  • 151
  • 1
  • 8
  • This method is unstable: 1. It takes about 4 sec for AppWidgetProvider to receive the broadcast; 2. Sometimes, AppWidgetProvider simply cannot receive the broadcast. Please used the method in http://stackoverflow.com/a/7738687/903665 – skyin Jan 21 '17 at 10:12
  • In addition to this solution, I repeated the functionality of the onCreate method in the onDataSetChanged method, creating another method to load the data and shared by both methods. – Armando Peña Mar 03 '19 at 00:25