这是我要调用StartActionMode的适配器类。我在setActionMode方法中调用它,但出现了以下错误:
无法从上下文转换为ActivityFragment。
类型的StartActionMode方法(ActivityFragment.ActionModeCallback)未定义
活动片段。

public class ListAdapter extends ArrayAdapter<ListGettersSetters>
{
    ArrayList<ListGettersSetters> arrayListGettersSetters;
    LayoutInflater layoutInflater;
    Context context;
    int Resource, i = 0, j = 0, checkedItemsCount = 0;
    Animation animation1;
    Animation animation2;
    CheckBox flipCheckBox;
    viewHolder holder;
    ActionMode actionMode;
    boolean isActionModeShowing;

    static class viewHolder
    {
        public CheckBox imageView;
        public TextView textViewName;
        public TextView textViewData;
        public TextView textViewDetails;
    }

    public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)
    {
        super(context, resource, arrayListGettersSetters);
        this.arrayListGettersSetters = arrayListGettersSetters;
        Resource = resource;
        this.context = context;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        animation1 = AnimationUtils.loadAnimation(context, R.anim.to_middle);
        animation2 = AnimationUtils.loadAnimation(context, R.anim.from_middle);

        isActionModeShowing = false;
    }

    @Override
    public int getCount()
    {
        return arrayListGettersSetters.size();
    }

    @Override
    public ListGettersSettersgetItem(int position)
    {
        return arrayListGettersSetters.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        holder = new viewHolder();
        if(convertView == null)
        {
            convertView = layoutInflater.inflate(Resource, null);

            holder.imageView = (CheckBox) convertView.findViewById(R.id.id_for_checkBox);
            holder.textViewName = (TextView) convertView.findViewById(R.id.id_for_name_textView);
            holder.textViewData = (TextView) convertView.findViewById(R.id.id_for_data_textView);
            holder.textViewDetails = (TextView) convertView.findViewById(R.id.id_for_details_textView);

            convertView.setTag(holder);
        }
        else
        {
            holder = (viewHolder) convertView.getTag();
        }

        holder.textViewName.setText(getItem(position).getName());
        holder.textViewData.setText(getItem(position).getData());
        holder.textViewDetails.setText(getItem(position).getDetails());
        holder.imageView.setTag(position);
        holder.imageView.setOnClickListener(new OnClickListener()
        {
            public void onClick(View view)
            {
                flipCheckBox = (CheckBox) view;
                flipCheckBox.clearAnimation();
                flipCheckBox.setAnimation(animation1);
                flipCheckBox.startAnimation(animation1);
                setAnimListners(arrayListGettersSetters.get(Integer.parseInt(view.getTag().toString())));
            }
        });

        return convertView;
    }

    private void setAnimListners(final ListGettersSetters listGettersSetters)
    {
        AnimationListener animationListener = new AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {
                if (animation == animation1)
                {
                    flipCheckBox.clearAnimation();
                    flipCheckBox.setAnimation(animation2);
                    flipCheckBox.startAnimation(animation2);
                }
                else
                {
                    listGettersSetters.setIsChecked(!listGettersSetters.isChecked());
                    setCount();
                    setActionMode();
                }
            }

            public void setCount()
            {
                if (listGettersSetters.isChecked())
                {
                    checkedItemsCount++;
                }
                else
                {
                    if (checkedItemsCount != 0)
                    {
                        checkedItemsCount--;
                    }
                }

                Log.v("Checked items count", checkedItemsCount + "");
            }

            private void setActionMode()
            {
                if (checkedItemsCount > 0)
                {
                    if (!isActionModeShowing)
                    {
                        actionMode = ((ActivityFragment) context).startActionMode(new ActivityFragment.ActionModeCallback(context));
                        isActionModeShowing = true;
                    }
                }
                else if (actionMode != null)
                {
                    actionMode.finish();
                    isActionModeShowing = false;
                }

                if (actionMode != null)
                {
                    actionMode.setTitle(String.valueOf(checkedItemsCount));
                }

                notifyDataSetChanged();
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {
            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
            }
        };
        animation1.setAnimationListener(animationListener);
        animation2.setAnimationListener(animationListener);
    }

这是我的activityfragment类,我在其中实现了一个名为actionmodecallback的类,该类在适配器类中调用。同样,当我在这个内部类中获取activityfragment的上下文时,也会得到相同的错误。
public class ActivityFragment extends ListFragment
{
    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState)
    {
        View view = layoutInflater.inflate(R.layout.folders_fragment_listview, null, false);

        return view;
    }

    public static final class ActionModeCallback implements ActionMode.Callback
    {
        Context context;

        public ActionModeCallback(Context context)
        {
            this.context = context;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
        {
            Toast toast = null;
            ArrayList<FoldersFragmentGettersSetters> selectedListItems = new ArrayList<FoldersFragmentGettersSetters>();

            StringBuilder selectedItems = new StringBuilder();

            for (FoldersFragmentGettersSetters foldersFragmentGettersSetters : ((ActivityFragment ) context).listAdapter.arrayListGettersSetters)
            {
                if (foldersFragmentGettersSetters.isChecked())
                {
                    selectedListItems.add(foldersFragmentGettersSetters);
                }
            }

            if (menuItem.getTitle().equals("Delete"))
            {
                toast = Toast.makeText(context, "Delete: " + selectedItems.toString(), Toast.LENGTH_SHORT);
            }
            else if (menuItem.getTitle().equals("Archive"))
            {
                toast = Toast.makeText(context, "Archive: " + selectedItems.toString(), Toast.LENGTH_SHORT);
            }
            else if (menuItem.getTitle().equals("Mark unread"))
            {
                toast = Toast.makeText(context, "Mark unread: " + selectedItems.toString(), Toast.LENGTH_SHORT);
            }
            else if (menuItem.getTitle().equals("Move"))
            {
                toast = Toast.makeText(context, "Move: " + selectedItems.toString(), Toast.LENGTH_SHORT);
            }
            else if (menuItem.getTitle().equals("Remove star"))
            {
                toast = Toast.makeText(context, "Remove star: " + selectedItems.toString(), Toast.LENGTH_SHORT);
            }
            if (toast != null)
            {
                toast.show();
            }
            actionMode.finish();
            return true;
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
        {
            menu.add("Delete").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            menu.add("Archive").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            menu.add("Mark unread").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            menu.add("Move").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            menu.add("Remove star").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode)
        {
            ((ActivityFragment ) context).inboxAdapter.checkedItemsCount = 0;
            ((ActivityFragment ) context).inboxAdapter.isActionModeShowing = false;
            for (FoldersFragmentGettersSetters foldersFragmentGettersSettersItem : ((InboxFragment) context).inboxList)
            {
                foldersFragmentGettersSettersItem.setIsChecked(false);
            }
            ((ActivityFragment ) context).listAdapter.notifyDataSetChanged();

            Toast.makeText(context, "Action mode closed", Toast.LENGTH_SHORT).show();
        }

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

最佳答案

从中修改listadapter的构造函数

public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)


public ListAdapter(ActivityFragment context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)

07-27 13:40