Brandon
Brandon

Reputation: 703

Widget Not Updating Views

So basically my Widget updates, according to my LogCat, and the TextView in my Widget's Layout is not. It updates when it first starts, but what I am trying to do is update it artificially using an intent after a BrocastReceiver. The intent works fine, the onUpdate runs after the onReceive, but the text is not updated. I have a feeling it has to do with the fact that a different context, AppWidgetManager, or appWidgetIds are being used.

Here are my methods:

String sent = "0";

@Override
public void onReceive(Context context, Intent intent){
    String action = intent.getAction();
    if(action != null && action.equals("27")){
        Log.i("Updating", "UPDATING WIDGET");
        sent = intent.getStringExtra("Sent");
        onUpdate(context,AppWidgetManager.getInstance(context),
                new int[]{0});
    }
    super.onReceive(context, intent);
}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds){
    Log.i("Widget","onUpdate");
    String begin = "Sent ";
    String end = " Texts Since Last Visit";
    final int N = appWidgetIds.length;
    for(int i = 0 ; i < N; i++){
        int widgetId = appWidgetIds[i];

        Intent intent = new Intent(context,History.class);
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);

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

        views.setOnClickPendingIntent(R.id.LLwidget, pi);
        Log.i("Widget","Sent: " + sent);
        views.setTextViewText(R.id.SentTextView, begin + sent + end);
        appWidgetManager.updateAppWidget(widgetId, views);
    }
}

Manifest:

 <receiver android:name="MyWidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/widget_info"/>
    </receiver>

widget_info:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:updatePeriodMillis="30000" 
  android:initialLayout="@layout/widget_layout" 
  android:minHeight="72dip" 
  android:minWidth="146dip">

</appwidget-provider>

I think that is all the necessary information considering I know for sure that the intent works and the onUpdate runs.

Hopefully someone can help me because this is my first attempt at a widget and my brain is a little frazzled because of lack of sleep over this. Thanks, Brandon

Upvotes: 1

Views: 1011

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

You are passing in 0 as an app widget ID. More than likely, that is not a valid app widget ID. Either use an appropriate app widget ID (e.g., one you put in an extra of the Intent you attach to the click event) or use the version of updateAppWidget() that does not take an app widget ID.

Upvotes: 2

Related Questions