对特定视图使用上下文操作栏时无法解析方法 startActionMode()

Cannot resolve method startActionMode() while using Contextual Action Bar for a particular view

我一直在尝试在复选框上使用上下文操作栏单击以从列表视图中删除多行...列表视图的每一行都有一个复选框和一个文本视图。在适配器 class 中,我调用 startActionMode() 如下:

**@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();
        }
    }**

适配器 class 扩展了 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;


    }

但问题是,当我调用 startActionMode() 时出现错误。它说无法解析该方法。是不是因为我是从 getView 方法中调用的……我很无助……任何回应都将不胜感激。提前谢谢你。

方法 startActionMode 是为 Activity 定义的,但没有为 Context 定义(参见 the documentation)。

在您的第一段代码中,如果您将 Activity 作为上下文传递,您可以将其转换为 Activity,如下所示:

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

或更改声明

Context context;

至:

Activity context;

根据评论编辑:

如果您从 Fragment 中的 View.OnClickListener() 开始 ActionMode.Callback,您可以检索 Activity 此片段当前与使用 [=23] 关联=]:

YourFragmentClass.this.getActivity().startActionMode(...);