Android 使用intent.putExtra将数据传递到小部件ListView不起作用。(使用RemoteViewsFactory)

Android 使用intent.putExtra将数据传递到小部件ListView不起作用。(使用RemoteViewsFactory),android,android-studio,android-intent,android-listview,android-widget,Android,Android Studio,Android Intent,Android Listview,Android Widget,我目前正在使用AppWidgetProvider、RemoteViewsFactory和RemoteViewsService创建具有ListView的主屏幕小部件 点击RecipeItemClick 此方法连接到每个配方卡视图,当用户单击其中一张卡时,用户输入该配方的活动,小部件应使用所单击配方的成分进行更新 public void onRecipeItemClick(Recipe recipe) { // some code to navigate to different activity

我目前正在使用AppWidgetProvider、RemoteViewsFactory和RemoteViewsService创建具有ListView的主屏幕小部件

点击RecipeItemClick

此方法连接到每个配方卡视图,当用户单击其中一张卡时,用户输入该配方的活动,小部件应使用所单击配方的成分进行更新

public void onRecipeItemClick(Recipe recipe) {
// some code to navigate to different activity
        updateWidget(this, recipe);
}
updateWidget

getComponents()返回成分对象的列表,该列表作为额外的意图传递

    public static void updateWidget(Context context, Recipe recipe){
        Intent intent = new Intent(context, RecipeAppWidgetProvider.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra("ingredients", (Serializable) recipe.getIngredients());
        int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, RecipeAppWidgetProvider.class));
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
        context.sendBroadcast(intent);
    }
收到后

在provider类中,我有一个变量'components',它的值是从SerializableExtraof intent更新的。从日志语句中,我已确认通过onReceive方法将正确的配料对象列表存储到“配料”变量中

private List<Ingredient> ingredients;

public void onReceive(Context context, Intent intent) {
        if(intent.getSerializableExtra("ingredients") != null){
            ingredients = (List<Ingredient>) intent.getSerializableExtra("ingredients");
            AppWidgetManager mgr = AppWidgetManager.getInstance(context);
            ComponentName cn = new ComponentName(context, RecipeAppWidgetProvider.class);
            mgr.notifyAppWidgetViewDataChanged(mgr.getAppWidgetIds(cn),R.id.widget_list_view);
        }
        super.onReceive(context, intent);
    }
这就是我将成分对象列表从MainActivity传递到provider和RemoteViewFactory的方法。我认为我的代码可以运行updateWidget()->onReceive()->onUpdate()。但是,从RemoteViewsService类的“onGetViewFactory”方法中记录intent.getSerializableExtra(“成分”)时,由于某种原因,数据会丢失。此外,虽然每次用户单击配方卡项目时都会调用“onUpdate”,但只有当我返回主屏幕时才会调用“onGetViewFactory”

onGetViewFactory 基本上,上述代码部分的数据丢失,我无法从intent.getSerializableExtra获取成分对象列表

public RemoteViewsFactory onGetViewFactory(Intent intent) {
        List<Ingredient> ingredients = (List<Ingredient>) intent.getSerializableExtra("ingredients");
        if (ingredients != null){
            Log.d("ingredients",ingredients.get(0).getIngredient());
        }
        return new RecipeWidgetRemoteViewsFactory(this.getApplicationContext(), intent);
    }
公共远程视图工厂onGetViewFactory(意图){
列表成分=(列表)intent.getSerializableExtra(“成分”);
如果(成分!=null){
Log.d(“配料”,配料.get(0.getComponent());
}
返回新的RecipeWidgetMoteViewsFactory(this.getApplicationContext(),intent);
}
到目前为止,provider类中的onUpdate()方法是通过成分对象列表正确调用的。当我将这些对象作为intent Extra并尝试在RemoteView.setRemoteAdapter中传递它们时,出现了一些问题。提前感谢您阅读本文。你的任何建议都能救我一命。为了更好地理解,请随意克隆我的项目并运行

public RemoteViewsFactory onGetViewFactory(Intent intent) {
        List<Ingredient> ingredients = (List<Ingredient>) intent.getSerializableExtra("ingredients");
        if (ingredients != null){
            Log.d("ingredients",ingredients.get(0).getIngredient());
        }
        return new RecipeWidgetRemoteViewsFactory(this.getApplicationContext(), intent);
    }