Search code examples
androidlistviewandroid-widgetremoteview

Android failed to load homescreen widget with List view


I am currently writing a simple recipe app for a project and I have come across an issue for which I need help. Thanks in Advance.

Issue:
The Android widget will load perfectly without the list view being activated, but if it is activated it fails to load the whole widget.

The following is the code:
Remote Factory View

public class Widget_Service extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new Remote_Ingredients_Adapter(this.getApplicationContext(), intent);
    }

    public class Remote_Ingredients_Adapter implements RemoteViewsService.RemoteViewsFactory {

        private Context context;
        private ArrayList<Ingredients> ingredients_list;

        public Remote_Ingredients_Adapter(Context context, Intent intent) {
            this.context = context;
            this.ingredients_list = intent.getParcelableArrayListExtra(EasyBakeWidget.Intent_Ingredients);
        }

        @Override
        public void onCreate() { }

        @Override
        public void onDataSetChanged() { }

        @Override
        public void onDestroy() { }

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

        @Override
        public RemoteViews getViewAt(int i) {
            RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.ingredients_adapter_layout);
            Ingredients ingredient = ingredients_list.get(i);
            row.setTextViewText(R.id.tv_ingredient_name,ingredient.getIngredient());
            row.setTextViewText(R.id.tv_ingredient_quantity,ingredient.getQuantityWithMeasure());
            return row;
        }

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

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

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

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

Widget Provider Code

Intent rlv_intent = new Intent(context,Widget_Service.class);
rlv_intent.putParcelableArrayListExtra(Intent_Ingredients, recipe.getIngredients());
widget.setRemoteAdapter(R.id.lv_widget_ingredients,rlv_intent);

Full Widget Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/curved_border_with_fill"
    android:orientation="vertical"
    android:padding="@dimen/widget_margin">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/Padding_Large"
            android:layout_marginTop="@dimen/Padding_Medium">

            <ImageButton
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:id="@+id/widget_back_button"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_keyboard_arrow_left"
                android:layout_alignBaseline="@id/widget_forward_button"
                android:background="@android:color/transparent"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/widget_recipe_name"
                android:textSize="25sp"
                tools:text="recipe_name"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@id/widget_back_button"
                android:layout_toStartOf="@id/widget_forward_button"
                android:layout_alignBaseline="@id/widget_back_button"
                android:gravity="center"/>

            <ImageButton
                android:layout_width="20dp"
                android:layout_height="24dp"
                android:id="@+id/widget_forward_button"
                android:layout_alignParentEnd="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_keyboard_arrow_right"
                android:background="@android:color/transparent"/>
        </RelativeLayout>

        <include layout="@layout/divider_line_solid"/>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/Padding_Large"
            android:id="@+id/lv_widget_ingredients"
            android:layout_gravity="center"/>
</LinearLayout>

List view layout (R.id.lv_widget_ingredients) NOTE this layout file is used in 2 places. The widget list view and a recycler view in one of the activities,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tv_ingredient_name"
        style="@style/Body_Custom"
        android:layout_weight="0.6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/Padding_Medium"
        android:textAlignment="textStart"
        android:maxLines="3"
        tools:text="Ingredient Name" />

    <TextView
        android:id="@+id/tv_ingredient_quantity"
        style="@style/Body_Custom"
        android:layout_weight="0.4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/Padding_Medium"
        android:textAlignment="textEnd"
        tools:text="Ingredient quantity" />
</LinearLayout>

Android Manifest Code

<receiver android:name=".components.EasyBakeWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/easy_bake_widget_info" />
        </receiver>

        <service
            android:name=".components.Widget_Service"
            android:permission="android.permission.BIND_REMOTEVIEWS" />

Solution

  • Changing my logcat to no filter mode allowed me to see the error which comes from the Home launcher. The error was

    Class not found when unmarshalling: com.andre_fernando.easybakerecipes.data_objects.Ingredients
                                                       java.lang.ClassNotFoundException: com.andre_fernando.easybakerecipes.data_objects.Ingredients
    

    So I converted the ArrayList of my custom ingredients model into 2 String ArrayList.

        Intent lv_intent = new Intent(context,Widget_Service.class);
    
        ArrayList<Ingredients> ingredients_list = recipe.getIngredients();
        ArrayList<String> ingredient_name = new ArrayList<>();
        ArrayList<String> ingredient_quantity = new ArrayList<>();
        for (Ingredients i: ingredients_list) {
            ingredient_name.add(i.getIngredient());
            ingredient_quantity.add(i.getQuantityWithMeasure());
        }
    
        lv_intent.putStringArrayListExtra("name",ingredient_name);
        lv_intent.putStringArrayListExtra("quantity",ingredient_quantity);
        widget.setRemoteAdapter(R.id.widget_ingredients_list,lv_intent);
    

    This fixed the widget.

    Also using a content provider to get the data also fixed the widget.