Android无法使用列表视图加载主屏幕小部件

时间:2018-02-25 14:05:39

标签: android listview android-widget remoteview

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

问题:
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 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" />

1 个答案:

答案 0 :(得分:1)

将我的logcat更改为无过滤模式,让我看到来自Home启动程序的错误。错误是

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

所以我将自定义成分模型的ArrayList转换为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);

这修复了小部件。

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