gpt4 book ai didi

android - 如何在 Activity 中显示 DialogFragment

转载 作者:行者123 更新时间:2023-12-02 04:44:43 25 4
gpt4 key购买 nike

我在 Activity 适配器的 Activity 上下文中显示 DialogFragment 时遇到问题。所以我有一个名为 AddDialogFragment 的自定义类,它扩展了 DialogFragment。当我用它在我的 fragment 适配器中显示该对话框时,一切都很顺利。我只是在那个 fragment 中那样做(上下文来 self 传递给 Adapter 的构造函数的 fragment :

FragmentActivity fragmentActivity = (FragmentActivity) context;
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
RecipeRemoveDialogFragment recipeDialogFragment = new RecipeRemoveDialogFragment();
recipeDialogFragment.show(fragmentManager, "recipeDialogFragment");

现在我想显示相同的 DialogFragment 但在 Activty 的适配器中。我是这样做的:

holder.setClickListener(new ItemClickListener2() {

@Override
public void onClick(View view, int position, boolean isLongClick) {
if (!isLongClick) {
// go to recipes site
} else {
RecipeItem recipeItem = recipeItems.get(position);
FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
RecipeAddDialogFragment recipeDialogFragment = new RecipeAddDialogFragment();
Log.d(TAG, "Ustawiono recipeUniqueId, coordinatorLayout oraz " +
"recipeDialogFragment w klasie RecipeAddDialogFragment");
recipeDialogFragment.setReferences(recipeItem.getRecipeUniqueID(),
coordinatorLayout, recipeDialogFragment);

Log.d(TAG, "Uruchamiam okno dialogowe RecipeAddDialogFragment");
recipeDialogFragment.show(fragmentManager, "recipeDialogFragment");
}
}

});

但当我单击适配器的列表项时它不起作用并且应用程序崩溃。出现错误:

12-05 18:17:47.700 8926-8926/com.example.nazwamarki.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nazwamarki.myapplication, PID: 8926
java.lang.ClassCastException: com.example.nazwamarki.myapplication.app.AppController cannot be cast to android.support.v4.app.FragmentActivity
at com.example.nazwamarki.myapplication.recipe.RecipeAdapter$1.onClick(RecipeAdapter.java:63)
at com.example.nazwamarki.myapplication.recipe.RecipeAdapter$ViewHolder.onLongClick(RecipeAdapter.java:119)
at android.view.View.performLongClick(View.java:4836)
at android.view.View$CheckForLongPress.run(View.java:19873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

编辑

上下文来自这里(我将其传递给Adapter 的 构造函数:

recipeAdapter = new RecipeAdapter(getApplicationContext(), recipeItems);

这是我所有的适配器以防万一:

  public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {

private static String TAG = RecipeAdapter.class.getSimpleName().toString();

private Context context;
private ArrayList<RecipeItem> recipeItems;
private CoordinatorLayout coordinatorLayout;

public RecipeAdapter(Context context, ArrayList<RecipeItem> recipeItems) {
this.context = context;
this.recipeItems = recipeItems;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item, parent,
false);

return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RecipeItem recipeItem = recipeItems.get(position);
Picasso.with(context).load(recipeItem.getRecipeImgThumbnailLink()).into(
holder.recipeItemImgThumbnail);
holder.recipeItemTitle.setText(recipeItem.getRecipeTitle());
holder.recipeItemKitchenMealType.setText("Kuchnia " + recipeItem.getRecipeKitchenType() +
", " + recipeItem.getRecipeMealType());
holder.recipeItemAddDate.setText(recipeItem.getRecipeAddDate());
holder.recipeItemLikeCount.setText(recipeItem.getRecipeLikeCount());
holder.setClickListener(new ItemClickListener2() {

@Override
public void onClick(View view, int position, boolean isLongClick) {
if (!isLongClick) {
// go to recipes site
} else {
RecipeItem recipeItem = recipeItems.get(position);
FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
RecipeAddDialogFragment recipeDialogFragment = new RecipeAddDialogFragment();
Log.d(TAG, "Ustawiono recipeUniqueId, coordinatorLayout oraz " +
"recipeDialogFragment w klasie RecipeAddDialogFragment");
recipeDialogFragment.setReferences(recipeItem.getRecipeUniqueID(),
coordinatorLayout, recipeDialogFragment);

Log.d(TAG, "Uruchamiam okno dialogowe RecipeAddDialogFragment");
recipeDialogFragment.show(fragmentManager, "recipeDialogFragment");
}
}

});
}

@Override
public int getItemCount() {
return recipeItems.size();
}

// Recipe Item Holder
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
View.OnLongClickListener {

private ImageView recipeItemImgThumbnail;
private TextView recipeItemTitle;
private TextView recipeItemKitchenMealType;
private TextView recipeItemAddDate;
private TextView recipeItemLikeCount;
private ItemClickListener2 clickListener2;

public ViewHolder(View itemView) {
super(itemView);
recipeItemImgThumbnail = (ImageView) itemView.findViewById(
R.id.recipe_item_img_thumbnail);
recipeItemTitle = (TextView) itemView.findViewById(R.id.recipe_item_title);
recipeItemKitchenMealType = (TextView) itemView.findViewById(
R.id.recipe_item_kitchen_meal_type);
recipeItemAddDate = (TextView) itemView.findViewById(R.id.recipe_item_add_date);
recipeItemLikeCount = (TextView) itemView.findViewById(R.id.recipe_item_like_count);

itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}

public void setClickListener(ItemClickListener2 itemClickListener2) {
this.clickListener2 = itemClickListener2;
}

@Override
public void onClick(View view) {
clickListener2.onClick(view, getAdapterPosition(), false);
}

@Override
public boolean onLongClick(View view) {
clickListener2.onClick(view, getAdapterPosition(), true);

return true;
}
}

public void setCoordinatorLayout(CoordinatorLayout coordinatorLayout) {
this.coordinatorLayout = coordinatorLayout;
}
}

最佳答案

改变

recipeAdapter = new RecipeAdapter(getApplicationContext(), recipeItems);

recipeAdapter = new RecipeAdapter(this, recipeItems);

适配器需要一个 Activity 上下文。如果您从 回调 中对其进行初始化,则使用 ClassName.this 而不是 this

关于android - 如何在 Activity 中显示 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34108465/

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