Divyang Patel
Divyang Patel

Reputation: 347

Widget Listview is not updating with notifyAppWidgetViewDataChanged

I am newbie in android development.I am Trying to load listview in my widget Using Web services. its loads perfectly but I want to updated my listview dynamically as my requirements.

Here attached my code:

 package com.widget;

 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.net.Uri;
 import android.util.Log;
 import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {

public static final String DATA_FETCHED = "com.widget.DATA_FETCHED";

public static String UPDATE_LIST = "UPDATE_LIST";
AppWidgetManager appWidgetManager;
ComponentName currentWidget;

public static Integer randomNumber;
SharedPreferences sharedprefer;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

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

        Intent serviceIntent = new Intent(context, RemoteFetchService.class);

        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds[i]);

        context.startService(serviceIntent);
        // appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i],
        // R.id.listViewWidget);

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



        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);

    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if (intent.getAction().equals(DATA_FETCHED)) {
        int appWidgetId = intent.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
        RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,
            R.id.listViewWidget);

    } 

}

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

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

    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);

   updateWidget(context, remoteViews);

    return remoteViews;
}

public static void updateWidget(Context context, RemoteViews remoteViews) {
    AppWidgetManager appWidgetManager = AppWidgetManager
            .getInstance(context);
    int appWidgetIds[] = appWidgetManager
            .getAppWidgetIds(new ComponentName(context,
                    WidgetProvider.class));
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,
                R.id.listViewWidget);

}
}

Upvotes: 3

Views: 2912

Answers (2)

almisoft
almisoft

Reputation: 2183

As so often in Android, use postDelayed:

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview);
    }

}, 1000);

Upvotes: 1

lenooh
lenooh

Reputation: 10682

In onReceive change your

if (intent.getAction().equals(DATA_FETCHED)) {

to

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

and it should work.

If it still does not work, replace the contents of the above if with:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), Your_Widget_Class_Here.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,R.id.listView);

Upvotes: 6

Related Questions