Android 无法使用 ListView 加载主屏幕小部件

标签 android listview android-widget remoteview

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

问题:
Android 小部件将在没有激活 ListView 的情况下完美加载,但如果它被激活,则无法加载整个小部件。

代码如下:
远程工厂查看

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>

ListView 布局(R.id.lv_widget_ingredients) 注意此布局文件用于 2 个地方。其中一个 Activity 中的小部件 ListView 和回收器 View ,

<?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 list 代码

<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

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

这修复了小部件。

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

关于Android 无法使用 ListView 加载主屏幕小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974336/

相关文章:

安卓 : How to use custom view in Widget [Remote Views]

java - 带有 float 按钮的 ScrollView

android - 从应用程序调用默认主屏幕

Android:在闹钟上播放自己的声音

android - 以编程方式在android中启用和禁用振动

java - 如何使用 Android 中 FirstActivity 的数据填充 SecondActivity 上的 ListView?

android - 如何删除 SQLITE 数据库中的第一行?

android - 在 Android 库项目中替换(或 "Override")字符串

WPF 在 Listview 中展开特定的扩展器

Android 小部件设计-默认黑色背景