There is an application in it CardView is displayed on top of RecyclerView . On CardView there is an element which, by pressing, knocks out a menu. It is necessary that AlertDialog by clicking on the menu AlertDialog , tried to do this:

 DialogFragment editDialog = new AlertDialogEdit(); editDialog.show(getFragmentManager(), " "); 

The problem is that in the adapter class where all this is prescribed for me, the studio does not want to create a getFragmentManager() method getFragmentManager() studio allocates it in red, although the same method in any other class is called normally. Please explain what may be wrong.

Adapter Code:

 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { private List<RecyclerItem> listItems; private Activity activity; DialogFragment editDialog = new AlertDialogEdit(); public RecyclerAdapter( Activity activity, List<RecyclerItem> listItems) { this.activity = activity; this.listItems = listItems; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { final Context context = holder.itemView.getContext();; holder.txtTitle.setText(listItems.get(position).getTitle()); holder.txtDescription.setText(listItems.get(position).getDescription()); holder.txtOptionDigit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popupMenu = new PopupMenu(context, holder.txtOptionDigit); popupMenu.inflate(R.menu.option_menu); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.mnu_item_edit: editDialog.show(getFragmentManager(), ""); Toast.makeText(context, "Edited", Toast.LENGTH_LONG).show(); break; case R.id.mnu_item_delete: listItems.remove(position); notifyDataSetChanged(); Toast.makeText(context, "Deleted", Toast.LENGTH_LONG).show(); break; default: break; } return false; } }); popupMenu.show(); } }); } @Override public int getItemCount() { return listItems.size(); } public class ViewHolder extends RecyclerView.ViewHolder{ public TextView txtTitle; public TextView txtDescription; public TextView txtOptionDigit; public ViewHolder(View itemView) { super(itemView); txtTitle = (TextView) itemView.findViewById(R.id.txtTitle); txtDescription = (TextView) itemView.findViewById(R.id.txtDescription); txtOptionDigit = (TextView) itemView.findViewById(R.id.txtOptionDigit); } }} 

The code for the class responsible for AlertDialog:

  public class AlertDialogEdit extends DialogFragment implements View.OnClickListener{ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.edit_dialog, null); v.findViewById(R.id.button).setOnClickListener(this); v.findViewById(R.id.button2).setOnClickListener(this); return v; } public void onClick(View v) { switch (v.getId()) { case R.id.button: Log.d("Log", "Ok"); dismiss(); break; case R.id.button2: Log.d("Log", "Cancel"); dismiss(); break; default: break; } }} 

    2 answers 2

    Please explain what may be wrong.

    When you call a method without specifying a class, this method must be defined either directly in the class from which the method is called, or in the class from which the current class is inherited, or in the interface that implements this class, there must be an implementation of this method by default ( default methods ).

    Since the getFragmentManager() method is not defined in either the RecyclerAdapter class or the RecyclerView.Adapter class, you get an error at the compilation stage, which says that this method is not defined.

    The getFragmentManager() method is available for the Activity class and its subclasses, and for the Fragment class and its subclasses.

    One of the methods to solve this problem:

    1. Define an interface with a method ( callback ) that will be called from the adapter when clicking on a certain element;
    2. Implement the interface defined in the first item in the activation (from there the method getFragmentManager() will be available);

      getFragmentManager() is an getFragmentManager() class method. Therefore, it is not available in the adapter class - it does not extend activations. You should, for good, create an interface listener for clicks, implement it in the activation / fragment, transfer it to the adapter and pull the method at the right time - it will be called in activation and this problem will not arise and will be more beautiful.

      As a temporary crutch, you can simply take the context of any view, map it to activation, and so call the method:

       ((AppCompatActivity)context).getFragmentManager()