2012-10-13 22 views
10

Ho cercato su Google e trovato il codice per manipolare i dati listview utilizzando RemoteViewsService e RemoteViewsService.RemoteViewsFactory. come di seguitoAndroid - Widget con Scrollable Listview Manuale Aggiorna

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 contiene

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

e scritto tutti i codici per il recupero dei dati da webservice in onCreate di WidgetViewsFactory implementa RemoteViewsService.RemoteViewsFactory

Per aggiornare i record manualmente o automaticamente ogni 5 secondi, ho trovato l'aggiornamento tramite i metodi di servizio come sotto menzionato

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

Come aggiornare automaticamente il listview .. So come aggiornare textview come accennato in precedenza.

risposta

48

È possibile aggiornare il ListView utilizzando il metodo notifyAppWidgetViewDataChanged() di AppWidgetManager. Si dovrà ottenere l'istanza di AppWidgetManager, ottenere le AppWidgetIds e la chiamata appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);

Pseudo codice,

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

Quando si chiama notifyAppWidgetViewDataChanged() allora il metodo di RemoteViewsFactoryonDataSetChanged() sarà chiamato, che serve come un adattatore per il tuo ListView. Puoi fare roba di servizi web e altre operazioni in onDataSetChanged(), ottenere la risposta e aggiungerla al set di dati che potrebbe essere ArrayList o qualsiasi raccolta di questo tipo.

Per ulteriori letture/riferimento è possibile controllare Keeping Collection Data Fresh parte dai documenti. Inoltre puoi controllare un esempio dimostrativo dal mio github.

+4

Tnx genious. :) –

+1

Dopo aver visto molti thread in questo sito e nessuno ha funzionato per me (probabilmente perché sto usando un "Collection Widget"), questo ha funzionato come un incantesimo! Grazie mille! –

+1

stavo usando sendBroadcast() che a sua volta stava ricreando ogni layout e non stava aggiornando le visualizzazioni del listino. Il metodo notifyAppWidgetViewDataChanged si occupa solo dell'aggiornamento rendendolo meno costoso. –

Problemi correlati