I am trying to implement an Android App widget with a ListView . I started with the Google documentation, but I was stuck in the list of displayed download types instead of the usual list items. Does anyone have experience viewing lists in widgets?
The code is the same as in Google Docs . I use ListView instead of StackView, and I use a database as the data source (which is not a problem, the data is received correctly - I tried this by returning the normal view of the list item with the correct data instead of the standard boot view)
A search on the Internet did not help me ... I also tried this tutorial , but it did not work either. I hope someone can help me, thanks in advance!
the code
WidgetProvider.java
public class WidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int i = 0; i < appWidgetIds.length; i++) { Intent intent = new Intent(context, EventWidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget); rv.setRemoteAdapter(android.R.id.list, intent); rv.setEmptyView(android.R.id.list, R.id.tvEmptyList); appWidgetManager.updateAppWidget(appWidgetIds[i], rv); } super.onUpdate(context, appWidgetManager, appWidgetIds); } }
WidgetService.java
public class EventWidgetService extends RemoteViewsService { @Override public RemoteViewsFactory onGetViewFactory(Intent intent) { return new EventsWidgetListProvider(getApplicationContext(), intent); } }
WidgetListProvider
public class EventsWidgetListProvider implements RemoteViewsFactory { private List<> _entities = new ArrayList<>(); private Context _context; // private int _appWidgetId; public EventsWidgetListProvider(Context context, Intent intent) { _context = context; // _appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); } @Override public void onCreate() { } private void refreshEntities() { // Request data from database _entities = RequestResult; } @Override public RemoteViews getViewAt(int position) { final RemoteViews remoteViews = new RemoteViews(_context.getPackageName(), R.layout.list_row); // Set data to remoteViews [remoteViews.setTextViewText(), remoteViews.setImageViewResource()] return remoteViews; } @Override public int getCount() { return _entities.size(); } @Override public long getItemId(int position) { return position; } @Override public void onDataSetChanged() { refreshEntities(); } @Override public void onDestroy() { } @Override public RemoteViews getLoadingView() { return new RemoteViews(_context.getPackageName(), R.layout.widget_loading); } @Override public int getViewTypeCount() { return 0; } @Override public boolean hasStableIds() { return false; } }