2012-10-13 18 views
10

我google搜索和發現代碼來操縱列表視圖數據使用RemoteViewsServiceRemoteViewsService.RemoteViewsFactory。如下Android - 可滾動列表視圖手動刷新

Intent svcIntent = new Intent(context, WidgetService.class); 
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]); 
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 
RemoteViews widget = new RemoteViews(context.getPackageName(),R.layout.widget_layout); 
widget.setRemoteAdapter(appWidgetIds[i], R.id.lstAppointments,svcIntent); 

WidgetService.java包含

public class WidgetService extends RemoteViewsService { 

    @Override 
    public RemoteViewsFactory onGetViewFactory(Intent intent) { 
     return (new WidgetViewsFactory(this.getApplicationContext(), intent)); 
    } 

    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return super.onBind(intent); 
    } 
} 

和寫的所有代碼中WidgetViewsFactory的的onCreate從web服務獲取數據實現RemoteViewsService.RemoteViewsFactory

了手動更新記錄或自動每5秒鐘,我發現通過服務方法更新如下

public class WordWidget extends AppWidgetProvider { 

    static int value = 1; 
    Handler handler = new Handler(); 
    MyRunnable myRunnable; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds) { 
     // To prevent any ANR timeouts, we perform the update in a service 
     myRunnable = new MyRunnable(context); 
     handler.postDelayed(myRunnable, 1000); 
    } 

    class MyRunnable implements Runnable { 

     Context context; 

     public MyRunnable(Context context) { 
      this.context = context; 
     } 

     public void run() { 
      context.startService(new Intent(context, UpdateService.class)); 
      handler.postDelayed(myRunnable, 1000); 
     } 
    } 

    public static class UpdateService extends Service { 
     @Override 
     public void onStart(Intent intent, int startId) { 
      // Build the widget update for today 
      RemoteViews updateViews = buildUpdate(this); 

      // Push update for this widget to the home screen 
      ComponentName thisWidget = new ComponentName(this, WordWidget.class); 
      AppWidgetManager manager = AppWidgetManager.getInstance(this); 
      manager.updateAppWidget(thisWidget, updateViews); 
     } 

     public RemoteViews buildUpdate(Context context) { 
      RemoteViews updateViews; 

      // Build an update that holds the updated widget contents 
      updateViews = new RemoteViews(context.getPackageName(), 
        R.layout.widget_word); 

      Log.e("value", String.valueOf(value)); 
      updateViews.setTextViewText(R.id.word_title, "Title"); 
      updateViews.setTextViewText(R.id.word_type, String.valueOf(value)); 
      updateViews.setTextViewText(R.id.definition, String.valueOf(value)); 

      value += 1; 

      // When user clicks on widget it opens www.google.com 
      Intent defineIntent = new Intent(Intent.ACTION_VIEW, 
        Uri.parse("https://www.google.co.in/")); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
        defineIntent, 0); 
      updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent); 

      return updateViews; 
     } 

     @Override 
     public IBinder onBind(Intent intent) { 
      // We don't need to bind to this service 
      return null; 
     } 
    } 
} 

如何自動更新列表視圖..我知道如何更新如上所述的textview。

回答

48

您可以使用notifyAppWidgetViewDataChanged()方法AppWidgetManager更新ListView。您將獲得AppWidgetManager的情況下,得到了AppWidgetIds和呼叫appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);

僞代碼,

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
int appWidgetIds[] = appWidgetManager.getAppWidgetIds(
          new ComponentName(context, WidgetProvider.class)); 
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview); 

當你調用notifyAppWidgetViewDataChanged()那麼RemoteViewsFactoryonDataSetChanged()方法將被調用這是作爲一個適配器爲你的ListView。您可以在onDataSetChanged()中執行網絡服務和其他操作,獲取響應並將其添加到您的數據集(可能是ArrayList或任何此類集合)。

如需進一步閱讀/參考,您可以從文檔中檢查Keeping Collection Data Fresh部分。你也可以從我的github查看演示例子。

+4

Tnx genious。 :) –

+1

看了這個網站的許多線程,並沒有爲我工作(可能是因爲我使用「集合小工具」),這一個工作作爲一種魅力!非常感謝你! –

+1

我正在使用sendBroadcast(),它反過來重新創建每個佈局,並沒有更新列表視圖。方法notifyAppWidgetViewDataChanged只負責刷新,使其更便宜。 –