3

I have problem with displaying DialogFragment from Activity context in activity adapter. So I have custom class called AddDialogFragment which extends DialogFragment. When I used it to display that dialog in my fragment's adapter everything went fine. I jus did it like that in that fragment (context was from my fragment passed to Adapter's constructor:

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

Now I want to display the same DialogFragment but inside Activty in it's adapter. I do it like that:

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");
        }
    }

});

but it does not work and app crashes when I click adapter's list item. The error appears:

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)

Edit

The context comes from here (I pass it to Adapter's constructor:

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

Here is my all adapter just in case:

  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;
    }
}
anton86993
  • 638
  • 1
  • 9
  • 26

2 Answers2

0

Change

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

to

recipeAdapter = new RecipeAdapter(this, recipeItems);

The adapter is expecting an Activity context. If you're initializing it from within a callback, then use ClassName.this, instead of this.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • Can you help also with not working `Back Navigation` in this screen? It appears but is not working when touching it. Don't know why? – anton86993 Dec 05 '15 at 18:38
  • 1
    Are you overriding the `onBackPressed()` method in your activity ? – Mohammed Aouf Zouag Dec 05 '15 at 18:39
  • I just set parent activity in `Manifest` and set `getSupportActionBar().setDisplayShowHomeEnabled(true)` `getSupportActionBar().setDisplayHomeAsUpEnabled(true)` in Activity. I alse gave in `onOptionsItemSelected` `if (id == R.id.home) NavUtils.navigateUpFromSameTask(this);` – anton86993 Dec 05 '15 at 18:43
  • Try calling `onBackPressed();` instead of `NavUtils.navigateUpFromSameTask(this);` – Mohammed Aouf Zouag Dec 05 '15 at 18:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97081/discussion-between-anton86993-and-jdev). – anton86993 Dec 05 '15 at 18:52
0

Try this

 void showVerifyDialog() {

        // Creating alert Dialog with one Button
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(Activity.this);

        // Setting Dialog Title
        alertDialog.setTitle("Forgot Password!");

        // Setting Dialog Message
        alertDialog.setMessage("Please Enter Your Email");
        final EditText input = new EditText(Activity.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        input.setLayoutParams(lp);
        alertDialog.setView(input);
        // alertDialog.setView(input);

        // Setting Icon to Dialog
        // alertDialog.setIcon(R.drawable.ic_launcher);
        // alertDialog.setCancelable(false);
        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                // Write your code here to execute after dialog

            }
        });
        // Setting Negative "NO" Button
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
Pavan Bilagi
  • 1,618
  • 1
  • 18
  • 23