我是android的noob,我遇到更新appwidget的問題。這是一個每20秒顯示不同文本的新聞小部件。在小部件初始化時,我可以正確地切換&顯示文字。但是,在每隔30分鐘更新一次widget後,我的widgetID int數組將保留更新前存在的int。所以在每次更新之後,小部件會顯示舊數據和新數據。是否有更新過程中清除舊數據的小部件ID int數組。任何幫助是極大的讚賞。如何正確更新AppWidget?
我的代碼:
在小部件切換文本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); }
}
也許如果你設置其他{storyCounter ++; } to storyConter = 0;會做的伎倆。 – k0sh 2013-02-21 16:15:08
感謝您的回覆。這將如何防止以前的widgetid的數據被顯示? – 2013-02-21 16:17:15
我從來沒有使用過小部件,但我使用位圖很多。當我想重置某個位圖時,我會調用bmp = null;我不確定你想在那裏做什麼。但你調用storyCounter ++;當storyCounter = 0時;並在你的其他聲明。這意味着你清除了你的storyCounter = 0;然後你調用storyCounter ++;爲什麼要這麼做 ?從邏輯上講,如果你在其他語句中你必須做不同的事情,但在你的事情中你做了兩次同樣的事情。 – k0sh 2013-02-21 16:22:21