我是 android 的菜鸟,我在更新 appwidget 时遇到问题。这是一个新闻小部件,每 20 秒显示不同的文本。初始化小部件时,让文本正确切换和显示没有问题。但是,每 30 分钟更新一次小部件后,我的 widgetID int 数组会保留更新之前存在的 int。因此,每次更新后,小部件都会显示旧数据和新数据。在更新过程中是否有清除旧数据的小部件 ID int 数组。任何帮助是极大的赞赏。
我的代码:
allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
在小部件中切换文本的方法。这最初工作正常,但更新后显示旧数据和新数据......
public void updateStory() {
tickerheadline = RssReader.rssheadline.get(storyCounter);
tickerstory = RssReader.rssstory.get(storyCounter);
remoteViews.setTextViewText(R.id.headline, tickerheadline );
remoteViews.setTextViewText(R.id.story, tickerstory );
if (storyCounter==RssReader.rssheadline.size()-1){
storyCounter = 0;
storyCounter++;
}else{
storyCounter++;
}
appWidgetManager.updateAppWidget(allWidgetIds, remoteViews);
//Log.e("Widget", allWidgetIds.toString());
mHandler.postDelayed(new Runnable() {
public void run() {
updateStory();
} } ,20000); }
}
编辑
public void updateStory() {
//Added
appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
ComponentName thisWidget = new ComponentName(getApplicationContext(),MyWidgetProvider.class);
remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(),R.layout.widget1);
tickerheadline = RssReader.rssheadline.get(storyCounter);
tickerstory = RssReader.rssstory.get(storyCounter);
remoteViews.setTextViewText(R.id.headline, tickerheadline );
remoteViews.setTextViewText(R.id.story, tickerstory );
if (storyCounter==RssReader.rssheadline.size()-1){
storyCounter = 0;
storyCounter++;
}else{
storyCounter++;
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
//appWidgetManager.updateAppWidget(allWidgetIds, remoteViews);
//Log.e("Widget", allWidgetIds.toString());
mHandler.postDelayed(new Runnable() {
public void run() {
updateStory();
} } ,20000); }
}