1

我一直在尝试在复选框单击上使用上下文操作栏从列表视图中删除多行...列表视图在每一行中都有一个复选框和一个文本视图。在适配器类中,我调用 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();
        }
    }**

Adapter 类扩展了 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 方法中调用的……我很无奈……任何回应都将不胜感激。先感谢您。

4

1 回答 1

6

该方法startActionMode是为Activity但未定义的Context(请参阅文档)。

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

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

或更改声明

Context context;

至:

Activity context;

从评论中编辑:

如果您ActionMode.Callback从 aView.OnClickListener()中的 a开始Fragment,您可以使用以下方法检索Activity当前与此片段相关联的片段getActivity()

YourFragmentClass.this.getActivity().startActionMode(...);
于 2015-05-11T11:57:02.057 回答