1

I have been trying to use Contextual Action Bar on checkBox click to delete multiple rows from a listView... The listview has a checkbox and a textview in each row. In the adapter class, I'm calling startActionMode() as follows:

**@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        View view=convertView;
        Log.v("ConvertView", String.valueOf(position));
        final ToPayModel state = toPayList.get(position);
        if (convertView == null)
        {
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.to_pay_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tvToPay);
            holder.check = (CheckBox) convertView.findViewById(R.id.toPaycheck);
            convertView.setTag(holder);
            //holder.name.setOnCheckedChangeListener();
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.check.setOnClickListener( new View.OnClickListener()
        {
            public void onClick(View v)
            {
                CheckBox cb = (CheckBox) v;
                ToPayModel toPayModel = (ToPayModel) cb.getTag();
                Toast.makeText(context, "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(),
                        Toast.LENGTH_LONG).show();
                toPayModel.setCheck(cb.isChecked());
                if(state.isCheck()){
                    mActionMode=context.startActionMode(new ActionBarCallback());
                }
                else{
                    mActionMode.finish();
                }
            }
        });

        //holder.code.setText(" (" + state.getCode() + ")");
        holder.name.setText(state.getName());
        holder.check.setTextColor(Color.BLACK);
        holder.check.setChecked(state.isCheck());
        holder.check.setTag(state);
        return convertView;
    }
    public static final class ActionBarCallback implements ActionMode.Callback{
        public ActionBarCallback(){
        }
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.contextual_menu,menu);
            return true;
        }
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("Checkbox Selected");
            return false;
        }
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId()){
                case R.id.delete_item:
                   // removeSelection();
                    return true;
                default: return false;
            }
        }
        @Override
        public void onDestroyActionMode(ActionMode mode) {
             mode.finish();
        }
    }**

The Adapter class extends BaseAdapter

public class ToPayListAdapter extends BaseAdapter {
    static Context context;
    LayoutInflater inflater;
    ActionMode mActionMode=null;
    public ArrayList<ToPayModel> toPayList;
    public ToPayListAdapter(Context context,ArrayList<ToPayModel> toPayList){

        this.context=context;
        this.toPayList=toPayList;


    }

But the problem is, there is an error when i call startActionMode(). It says cannot resolve the method. Is it because I'm calling from within the getView method... I'm helpless... Any response will be appreciated. Thank you in advance.

Anjali
  • 37
  • 2
  • 6

1 Answers1

6

The method startActionModeis defined for Activity but is not defined for Context (see the documentation).

In your first chunk of code, if you are passing an Activity as the context, you can cast it to Activity as follows:

mActionMode=((Activity)context).startActionMode(new ActionBarCallback());

Or change the declaration

Context context;

to:

Activity context;

Edit from the comments:

If you are starting the ActionMode.Callback from a View.OnClickListener() in a Fragment, you can retrieve the the Activity this fragment is currently associated with using getActivity():

YourFragmentClass.this.getActivity().startActionMode(...);
antonio
  • 18,044
  • 4
  • 45
  • 61
  • Thank you for the response..Actually the class i'm using extends Fragment... So would using ((Activity) context).startActionMode(new......) work? – Anjali May 11 '15 at 13:05
  • If your class extends `Fragment` then you can do `YourFragmentClass.this.getActivity().startActionMode(...);` – antonio May 11 '15 at 13:07
  • @antonio Nice solution. I have a RecyclerView list and am having trouble launching a Contextual Action Bar. I would appreciate any help or insights you could provide on the issue, here: https://stackoverflow.com/questions/47406498/recyclerview-how-to-start-contextual-action-bar-cab-with-a-checkbox – AJW Nov 22 '17 at 04:40