Search code examples
androidandroid-appcompatandroid-recyclerviewcontextual-action-bar

Android startActionMode cannot be resolved


I have followed a tutorial (https://www.youtube.com/watch?v=8XR0cy3YgLo), modified it slightly and then incorporated the onLongClickListener (https://www.youtube.com/watch?v=jGW95imi9tw) where all of the code works without errors except I am looking to use the CAB (Contextual Action Bar) in order to select multiple items and be able to delete the selected.

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

Context c;
ArrayList<Player> players;
ActionMode mActionMode;

...

//View Holder Initialised
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    //View obj
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);

    //Holder
    MyHolder holder=new MyHolder(v);
    return holder;
}

//Data is bound to views
@Override
public void onBindViewHolder(MyHolder holder, int position) {
    holder.nameTxt.setText(players.get(position).getName());
    holder.posTxt.setText(players.get(position).getPosition());
    holder.img.setImageResource(R.drawable.marker);

    //When view is clicked
    holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onItemClick(View v, int pos) {
            Snackbar.make(v,players.get(pos).getName() + " 1 ", Snackbar.LENGTH_SHORT).show();
        }
    });

    holder.setItemLongClickListener(new ItemLongClickListener() {
        @Override
        public void onItemLongClick(View v, int pos) {
            mActionMode = startActionMode(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mode.setTitle("Selected");

                    MenuInflater inflater = mode.getMenuInflater();
                    inflater.inflate(R.menu.contextual_menu, menu);
                    return true;
                }

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return true;
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_delete:
                            //deleteClicked();
                            mode.finish();
                            return true;
                        default:
                            //doneClicked();
                            return false;
                    }
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                }
            });
        }
    });
}

So the issue that I am experiencing is that 'mActionMode = startActionMode(new ActionMode.Callback() {' does not work as 'startActionMode' cannot be resolved.

I understand that startActionMode belongs to the AppCompat but I cannot understand how else to initialise the CAB. I have looked around and cannot seem to find a definitive answer to this so would appreciate any help with resolving this.

EDIT

I have now managed to get the CAB (Contextual Action Bar) to be shown through the long click handler (after following this tutorial: http://www.startingandroid.com/how-to-use-sqlite-database-in-android/) but I cannot work out how to actually get reference to the given item. As currently long clicking on any item will bring up the CAB but I do not have reference to the item that was clicked.

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

    v.setClickable(true);

    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "CLICK", Snackbar.LENGTH_SHORT).show();
        }
    });

    v.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Snackbar.make(v, "LONG CLICK", Snackbar.LENGTH_SHORT).show();
            if (mActionMode != null) {
                return false;
            }
            v.startActionMode(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    MenuInflater inflater = mode.getMenuInflater();
                    inflater.inflate(R.menu.context, menu);
                    return true;
                }

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    //get number of selected items

                    switch (item.getItemId()) {
                        case R.id.context_favourite:
                            //SQL - Favourite Item
                            mode.finish();
                        case R.id.context_delete:
                            //SQL - Delete Item
                            mode.finish();
                    }
                    return false;
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    mActionMode = null;
                }
            });
            return true;
        }
    });
    return userViewHolder;
}

Solution

  • startActionMode is method available only if your in an Activity or you have a reference of an activity to make that call.

    From your code, your actually calling it from your MyAdapter class which is not an Activity.