gpt4 book ai didi

java - setTextViewText 不更新小部件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:10:58 25 4
gpt4 key购买 nike

下面显示的是我的类(class)的简化版本。我在不更新小部件 TextView 的 onReceive 方法中遇到问题。它在 setTextViewText 之前的行上输出的 logcat 中显示了正确的信息。我不确定出了什么问题,一直在拔头发(而且我已经秃了)。

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 来更新小部件。我添加的代码如下所示。

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

关于java - setTextViewText 不更新小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4433464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com