Android未能使用列表视图加载房屋屏幕窗口小部件



我目前正在为一个项目编写一个简单的食谱应用程序,我遇到了一个我需要帮助的问题。提前致谢。

问题:
Android小部件将完美加载而不会激活列表视图,但是如果激活它将无法加载整个小部件。

以下是代码:
远程工厂视图

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;}
    }
}

小部件提供商代码

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);

完整的小部件布局

<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>

列表查看布局(r.id.lv_widget_ingredients(请注意,此布局文件在2个位置使用。其中一项活动中的小部件列表视图和回收器视图,

<?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清单代码

<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" />

将我的logcat更改为无滤波器模式,使我可以看到来自家用启动器的错误。错误是

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

所以我将自定义成分模型的阵列列表转换为2个字符串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);

这修复了小部件。

还使用内容提供商获取数据也修复了小部件。

最新更新