Android – Widget con lista desplegable Listview Manual Actualizar

Busqué en Google y encontré el código para manipular datos de listview usando RemoteViewsService y RemoteViewsService.RemoteViewsFactory. como a continuació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 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); } } 

Y escribió todos los códigos para buscar datos de webservice en onCreate de WidgetViewsFactory implementa RemoteViewsService.RemoteViewsFactory

Para actualizar los registros de forma manual o automática cada 5 segundos, encontré la actualización a través de los métodos de servicio como se menciona a continuación

 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ómo actualizar el listview automáticamente .. Sé cómo actualizar textview como se mencionó anteriormente.

Puede actualizar ListView utilizando el método notifyAppWidgetViewDataChanged () de AppWidgetManager . Tendrás que obtener la instancia de AppWidgetManager, obtener AppWidgetIds y la llamada appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);

Pseudo Código,

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

Cuando llama a notifyAppWidgetViewDataChanged() se onDataSetChanged() método onDataSetChanged() de RemoteViewsFactory que está sirviendo como un adaptador para su ListView. Usted puede hacer cosas de servicio web y otras operaciones en onDataSetChanged() , obtener la respuesta y agregarlo a su conjunto de datos que tal vez ArrayList o cualquier colección.

Para lecturas / referencias adicionales puede comprobar Mantener datos de recolección Parte fresca de los documentos. También puede comprobar un ejemplo de demostración de mi github .

  • ¿Cómo implementar widget de rueda como en el iPhone?
  • Escribir texto multilínea en el botón en Android
  • Picasso: cargar imágenes en widget listview
  • Cambiar el fondo de la sugerencia de AppCompat SearchView
  • ¿Cómo saber cuándo el widget de la galería deja de desplazarse?
  • Comportamiento extraño de drawableRight para Switch
  • Cómo configurar el enfoque a la derecha del texto en EditText para android?
  • Solución para AppWidgetProvider onEnabled & onDisabled no se está llamando
  • Haga que una cierta parte de un android-textview se alinee a la derecha
  • HorizontalListview con imagen seleccionar y unselect
  • Transmitir una matriz de enteros desde un AppWidget a un RemoteViewsService preexistente para ser renderizado por el RemoteViewsFactory
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.