Mehvish Ali
Mehvish Ali

Reputation: 752

How to get information about RecyclerView item from Context Menu

I have a RecyclerView for which I have implemented ContextMenu, which is triggered when onLongCLick event of the RecyclerView Item. But I am unabale to get the position of the RecyclerView item on which ContextMenu is created inside onMenuItemClick(MenuItem item).

Code inside my adapter class:

   public static class PlaceViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener,MenuItem.OnMenuItemClickListener {
        protected TextView vName;
        protected TextView vDes;
        protected ImageView vBanner;
        CheckPlacesFragment fragmentCtx;
        ArrayList<UserPlaces> places = new ArrayList<UserPlaces>();
        Context ctx;
        UserPlaces place;

        public PlaceViewHolder(View v, Context ctx, ArrayList<UserPlaces> places, CheckPlacesFragment fragmentCtx) {
            super(v);
            v.setOnClickListener(this);
            v.setOnCreateContextMenuListener(this);
            this.places = places;
            this.ctx = ctx;
            this.fragmentCtx = fragmentCtx;
            vName =  (TextView) v.findViewById(R.id.place_name);
            vDes = (TextView)  v.findViewById(R.id.place_des);
            vBanner = (ImageView) v.findViewById(R.id.banner_img);


        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
             place = this.places.get(position);
            Toast.makeText(this.ctx,place.getName(),Toast.LENGTH_SHORT).show();


        }


        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuItem deleteAction = menu.add("Delete");
            deleteAction.setOnMenuItemClickListener(this);
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {

           // int id = item.getItemId();
           // ERROR : How to get item position which ContextMenu Created
            switch (id) {
                case 0 :
                    //Call delete Function from Fragment
                    fragmentCtx.test(pid);
                    return true;
                case 1:

                    return true;

            }
            return false;
        }
    }

Upvotes: 2

Views: 1706

Answers (2)

Faisal Naseer
Faisal Naseer

Reputation: 4258

Replace your onMenuItemClick() with

public boolean onMenuItemClick(MenuItem item) {

    int id = item.getItemId();
    int recyclerId = getLayoutPosition();
    place = this.places.get(recyclerId);
    String pid=place.getId();
    switch (id) {
        case 0 :
            //Call delete Function from Fragment
            fragmentCtx.test(pid);
            return true;
        case 1:

            return true;

    }
    return false;
}

Upvotes: 3

R. Zag&#243;rski
R. Zag&#243;rski

Reputation: 20268

You can try ViewHolder's function getLayoutPosition().

Upvotes: 2

Related Questions