gpt4 book ai didi

java - 通过 Intent 将数据从 App 传递到 Widget 时的空指针

转载 作者:行者123 更新时间:2023-11-29 18:46:30 25 4
gpt4 key购买 nike

我有一个使用 Retrofit 检索的食谱列表。然后我得到食谱名称,我想更新与我的应用关联的小部件以显示所选的 recipeNamerecipeName 反射(reflect)在我的 RecyclerView 适配器中。

当用户点击 RecylcerView 中的项目时,我想更新应用程序的关联小部件。

下面是 RecyclerView 的适配器,其中我有 .setOnClickListener() 方法并通过 Intent 传递我的所有数据.

        @Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, final int position) {
String id = mRecipeDataSet.get(position).getId();
//The recipe name I want to pass to my widget
final String recipeName = mRecipeDataSet.get(position).getName();
holder.mTextView.setText(id);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(con, BakingWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(con).getAppWidgetIds(new ComponentName(con, BakingWidgetProvider.class));
if(ids != null && ids.length > 0) {
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
intent.putExtra(RECIPE_NAME, recipeName);
con.sendBroadcast(intent);
}
Intent i = new Intent(con, IndividualRecipeActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Recipe_List", (ArrayList<? extends Parcelable>) mRecipeDataSet);
i.putExtras(bundle);
i.putExtra("Position",position);
con.startActivity(i);
}
});

}

下面是管理我的小部件的 BakingWidgetProvider 类。每次用户点击 RecyclerView 中的新项目时,我都想更新小部件界面:

公共(public)类 BakingWidgetProvider 扩展 AppWidgetProvider {

private static final String RECIPE_NAME = "RECIPE_NAME";
public static final String ACTION_TEXT_CHANGED = "kitchenpal.troychuinard.com.kitchenpal.TEXT_CHANGED";
private static String mRecipeName;

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {

CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.baking_widget_provider);
Log.v("WIDGET", mRecipeName);
views.setTextViewText(R.id.selected_recipe, mRecipeName);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0,
intent, 0);
//TODO: Why isn't my widget launching my MainActivity onClick?
//TODO: I am trying to add a ListView of ingredients to my Widget
but have no idea where to begin
views.setOnClickPendingIntent(R.id.chef, pIntent);

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}

@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}

//TODO: I cannot pass data from MyAdapter to the BakingWidgerProvider. I am simply trying to display the most recently
//TODO: selected recipe on the widget. I have scoured StackOverflow to no avail
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_TEXT_CHANGED)) {
// handle intent here
mRecipeName = intent.getStringExtra(RECIPE_NAME);
}
super.onReceive(context, intent);

// if(intent.getAction().equals("UPDATE_ACTION")) {
// mRecipeName = intent.getStringExtra(RECIPE_NAME);
// int[] appWidgetIds =


intent.getExtras().getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
// if (appWidgetIds != null && appWidgetIds.length > 0)
{
// this.onUpdate(context,
AppWidgetManager.getInstance(context), appWidgetIds);
// }
// } else {
// super.onReceive(context, intent);
// }
// }
}
}

更新:我已经包含了用于包含 .onReceive() 覆盖的代码,并且我在此覆盖方法中设置了字段。然后我尝试使用它来更新我在 .onUpdateAppWidget() 中的界面,但是小部件没有更新基于 mRecipeName 字段的 TextView,因为 mRecipeName 字段为空。

最佳答案

您应该在 AppWidgetClass 中覆盖 onReceive。这会接收广播的 Intent ,因此您可以从中检索数据

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
//Insert code here
}

然后继续添加您的代码以从 Intent 接收/分配数据。

关于java - 通过 Intent 将数据从 App 传递到 Widget 时的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51677018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com