setTextViewText没有更新

setTextViewText没有更新

本文介绍了setTextViewText没有更新插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示的是我班的简化版本。我有在没有更新的窗口小部件的TextView中的onReceive方法麻烦。它示出了其中被输出在setTextViewText前行的logcat正确的信息。我不知道什么是错,并已拉出我的头发(我已经秃顶)。

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+ "\n"+ amount+"\"\n"+ 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+ "\n"+ amount+"\"\n"+ condition);
            // This never updates my widget
            remoteViews.setTextViewText(R.id.snowwidget,  mtn_name+ "\n"+ amount+"\"\n"+ condition);

        }
    }

    super.onReceive(context, intent);
}

}

推荐答案

找到了答案。在调用后 remoteViews.setTextViewText ,你需要通过调用 updateAppWidget 更新窗口小部件。在code我加了如下图所示。

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

这篇关于setTextViewText没有更新插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:52