Home » Android » ListView on widget doesn't show up onUpdate()

ListView on widget doesn't show up onUpdate()

Posted by: admin November 1, 2017 Leave a comment

Questions:

I try to make home screen widget to show listView with a few elements. I read documentation and tutorial, wrote codes but it seems doesn’t work, items on widget don’t show up. When I debug it, I see “Loading…” on items, but after loading it disappeared. Any advice? As you can see on screenshot Button is visible, but listView not and textView also not.

widget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ListView
        android:id="@+id/widget_list"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:weightSum="100">

        <TextView
            android:id="@+id/widget_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="90"
            android:text="@string/widget_last" />

        <Button
            android:id="@+id/widget_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="@string/widget_refresh" />
    </LinearLayout>

</LinearLayout>

route_widget_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/widget_route_opoznienie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="TextView" />

    <TextView
        android:id="@+id/widget_route_nastepny"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="TextView" />

    <TextView
        android:id="@+id/widget_route_kierunek"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="TextView" />
</LinearLayout>

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, WidgetService.class);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, i);

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
            remoteViews.setRemoteAdapter(R.id.widget_list, intent);

            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

WidgetService.java

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return (new ListProvider(this.getApplicationContext()));
    }
}

ListProvider.java

public class ListProvider implements RemoteViewsService.RemoteViewsFactory {

    private List<VehicleDetails> list;
    private Context context;

    public ListProvider(Context applicationContext) {
        this.context = applicationContext;
    }

    @Override
    public void onCreate() {
        list = new ArrayList<>();
    }

    @Override
    public void onDataSetChanged() {
        populateListItem();
    }

    @Override
    public void onDestroy() {
        list.clear();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public RemoteViews getViewAt(int i) {
        final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.route_view_widget);
        VehicleDetails listItem = list.get(i);
        remoteView.setTextViewText(R.id.widget_route_opoznienie, listItem.getTime().toString());
        remoteView.setTextViewText(R.id.widget_route_nastepny, listItem.getNextStop());
        remoteView.setTextViewText(R.id.widget_route_kierunek, listItem.getDestination());

        return remoteView;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    private void populateListItem() {
       //some List<VehicleDetails> list loading
    }
}

Loading items on widget

After loading

Answers: