Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

java - setTextViewText not updating widget

Shown below is a simplified version of my class. I am having trouble in the onReceive method which isn't updating the widget TextView. It shows the correct information in the logcat which is outputted on the line before the setTextViewText. I'm not sure whats wrong and have been pulling my hair out (and I'm already balding).

public class SnowWidget extends AppWidgetProvider {

public static List<Article> mymtns = new ArrayList<Article>();
public static RemoteViews remoteViews;
public static ComponentName thisWidget;

public static String amount = "";
public static String mtn_name = "";
public static String desc = "";
public static String condition = "";
public static String[] type;

public static int index = 0;

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

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

    thisWidget = new ComponentName(context, SnowWidget.class);

    // This one works fine
    remoteViews.setTextViewText(R.id.snowwidget,  mtn_name+ "
"+ amount+""
"+ condition);

    /* Make the buttons work */

Intent next = new Intent(context, SnowWidget.class);
next.setAction(ACTION_WIDGET_RECEIVER);

PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 0, next, 0);
remoteViews.setOnClickPendingIntent(R.id.nextMtn, nextPendingIntent);

/* END - Make the buttons work */

    appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}

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

    // check, if our Action was called
    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
        if(mymtns.size() > 0)
        {

            // This show up correctly in logcat
            Log.d("onReceive", "New Info => "+ mtn_name+ "
"+ amount+""
"+ condition);
            // This never updates my widget
            remoteViews.setTextViewText(R.id.snowwidget,  mtn_name+ "
"+ amount+""
"+ condition);

        }
    }

    super.onReceive(context, intent);
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found the answer. After calling the remoteViews.setTextViewText you need to update the widget with a call to updateAppWidget. The code I added is shown below.

AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...