2012-10-13 34 views
10

Tôi đã tìm kiếm và tìm mã để thao tác dữ liệu danh sách sử dụng RemoteViewsServiceRemoteViewsService.RemoteViewsFactory. như sauAndroid - Widget có Chế độ xem danh sách cuộn có thể cuộn

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 chứa

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); 
    } 
} 

và viết tất cả các mã cho việc lấy dữ liệu từ webservice trong onCreate của WidgetViewsFactory thực hiện RemoteViewsService.RemoteViewsFactory

Đối với việc cập nhật hồ sơ bằng tay hoặc tự động cứ sau 5 giây, tôi tìm thấy các phương thức Cập nhật qua Dịch vụ như sau được đề cập

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; 
     } 
    } 
} 

Cách tự động cập nhật listview .. Tôi biết cách cập nhật chế độ xem văn bản như đã đề cập ở trên.

Trả lời

48

Bạn có thể cập nhật ListView bằng cách sử dụng phương thức notifyAppWidgetViewDataChanged() của AppWidgetManager. Bạn sẽ phải có được sự thể hiện của AppWidgetManager, có AppWidgetIds và cuộc gọi appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);

Pseudo Code,

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

Khi bạn gọi notifyAppWidgetViewDataChanged() sau đó phương pháp onDataSetChanged() của RemoteViewsFactory sẽ được gọi là phục vụ như là một Adapter cho ListView của bạn. Bạn có thể làm công cụ dịch vụ web và các hoạt động khác trong onDataSetChanged(), nhận phản hồi và thêm nó vào tập dữ liệu của bạn, có thể là ArrayList hoặc bất kỳ Bộ sưu tập nào như vậy.

Để đọc thêm/tham khảo, bạn có thể kiểm tra Keeping Collection Data Fresh một phần từ tài liệu. Ngoài ra, bạn có thể kiểm tra một ví dụ demo từ github của tôi.

+4

Tnx genious. :) –

+1

Sau khi xem nhiều chủ đề trong trang web này và không có tác phẩm nào cho tôi (có lẽ vì tôi đang sử dụng "Bộ sưu tập Widget"), cái này hoạt động như một sự quyến rũ! Cảm ơn nhiều! –

+1

tôi đã sử dụng sendBroadcast() mà lần lượt là tái tạo từng bố cục và không cập nhật danh sách xem. Phương pháp thông báoAppWidgetViewDataChanged chỉ chăm sóc làm mới làm cho nó rẻ hơn. –

Các vấn đề liên quan