-1
public class yeniAdapter extends RecyclerView.Adapter<yeniAdapter.MyViewHolder> {
    ArrayList<Urun> urunListesi;
    private Context mContext;
    private OnItemClickListener mListener;

    public yeniAdapter(Context context, List<Urun> urunListesi) {
        mContext = context;
        this.urunListesi = (ArrayList<Urun>) urunListesi;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(mContext).inflate(R.layout.custom_satir, parent, false);
        MyViewHolder holder = new MyViewHolder(v);
        return holder;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        Urun urunBilgileri = urunListesi.get(position);
        String yorumyapanId=urunBilgileri.getKisiId();
        holder.kisiIsim.setText(urunBilgileri.getKisiAd());
        holder.urunYorum.setText(urunBilgileri.getUrunYorum());

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
    View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener{

        TextView kisiIsim,urunYorum;


        public MyViewHolder(View itemView) {
            super(itemView);

            kisiIsim = (TextView) itemView.findViewById(R.id.ad);
            urunYorum = (TextView) itemView.findViewById(R.id.yorum);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            mListener.onItemClick(position);
                        }
                    }
                }
            });
            itemView.setOnClickListener(this);
            itemView.setOnCreateContextMenuListener(this);

        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {

            if (mListener != null) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {

                    switch (item.getItemId()) {
                        case 1:
                            mListener.onWhatEverClick(position);
                            return true;
                        case 2:
                            mListener.duzenleClick(position);
                            return true;
                    }
                }
            }
            return false;
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    mListener.onItemClick(position);
                }
            }
        }


        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

            menu.setHeaderTitle("Ne Yapmak İstiyorsun?");
            MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Seç");
            MenuItem duzenle = menu.add(Menu.NONE, 2, 2, "Düzenle");

            doWhatever.setOnMenuItemClickListener(this);
            duzenle.setOnMenuItemClickListener(this);
        }

        }


    public interface OnItemClickListener {

        void onItemClick(int position);

        void onWhatEverClick(int position);

        void duzenleClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    }

I have an application where the user can login. Users can comment. On the Comments page, if the comment belongs to the person himself, he will see the options in Menuitem that he gets from the customm adapter. If the person making the comment is not his own, he will see another option when he clicks on it and the other options will be hidden. How can I do it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
coding123
  • 1
  • 1
  • 6
  • Possible duplicate of [How do I hide a menu item in the actionbar?](https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar) – Himanshu Arora May 12 '19 at 15:53
  • My menu item is being created in the adapter. Fragment implementation and calling? @HimanshuArora – coding123 May 12 '19 at 15:56
  • That's a logic which you can implement with the list you have and hide the visibility of the menu option item accordingly. – Himanshu Arora May 12 '19 at 16:03

2 Answers2

0

I don't know if you really need an adapter for your menu, the common way for that kind of Menu is an xml file under res/menu which looks like this :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:id="@+id/menu_edit_recipe"
            android:title="Edit"/>

    <item
            android:id="@+id/option_one"
            android:title="Option"></item>


</menu>

In your activity just override this method, check your condition, and set the visibility

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        val yourMenuItem = menu!!.findItem(R.id.option_one)

        val yourCondition = true

        yourMenuItem.isVisible = !yourCondition


        return true
    }
0

Could you please try using the following line in your onCreateContextMenu after you add the menu items,

  doWhatever.setVisible(false);

or

menu.findItem(1).setVisible(false);

You can put a condition in onCreateContextMenu after adding items and place the above lines(either of them) to hide the menu item of context menu. Please try and let me know.