【发布时间】:2013-02-07 01:18:54
【问题描述】:
我是 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); }
}
【问题讨论】:
-
也许如果你设置 else { storyCounter++; } to storyConter=0;会成功的。
-
感谢您的回复。这将如何防止显示以前的 widgetid 数据?
-
我以前从未使用过小部件,但我经常使用位图。当我想重置某些位图时,我会调用 bmp = null;我不确定你想在那里做什么。但是你打电话给storyCounter++;当故事计数器=0;并在您的 else 声明中。这意味着你清除了你的 storyCounter = 0;然后你调用了storyCounter++;为什么要这么做 ?从逻辑上讲,在您的 if else 语句中,您必须做不同的事情,但在您的语句中,您要做两次相同的事情。
-
我使用计数器来跟踪我从数组列表中提取的字符串。当我将它重置为零,然后重新启动计数器时,我确保在达到数组列表中的最后一个值后文本会不断切换。
标签: android android-widget int updates android-appwidget