Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Widget with ListView displaying Loading views only

I am trying to implement an Android App Widget with a ListView. I started by following the Google documentation but I got stuck with a list of loading views being displayed instead of the normal list items. Does anybody have experiences with list views in widgets?

The code is the same as in the Google docs. I use a ListView instead of StackView and as data source I use a database (which isn´t the problem, data is obtained correctly - I have tried that by returning a normal list item view with correct data instead of the default loading view)

Searching the internet didn´t help me... I also tried out this tutorial but it did not work either. I hope anybody can help me, thanks in advance!

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;
    }
}
like image 471
sschmid Avatar asked Sep 28 '14 20:09

sschmid


1 Answers

Your getViewTypeCount returns 0. Per the documentation:

If the adapter always returns the same type of View for all items, this method should return 1.

By returning 0, you're telling the system that you have no view types to show.

like image 196
ianhanniballake Avatar answered Nov 13 '22 02:11

ianhanniballake