Passing id of ListView item to ActionMode.Callback object

醉酒当歌 提交于 2019-12-12 12:49:58

问题


So my problem right now is that right now I am long clicking an item in a ListView which brings up a contextual action bar. The id passed into onItemLongClick is the variable that I would like to use in the mActionModeCallback's on ActionItemClicked() method. This seems like it would be a fairly common procedure since if a user is editing a list of items, you would want to access the id of that row in the database somehow when the user clicked an "edit" or a "delete" action.

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> p, View view, int pos, long id) {

        //The id of the row in the database
        long variableThatIWantToPassToCallback = id; 
        mActionMode = getActivity().startActionMode(mActionModeCallback);
        view.setSelected(true);
        return true;
    }
});

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {}

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {}

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        //I would like access to the id of the clicked item here, NOT item.getItemId()
    }

    public void onDestroyActionMode(ActionMode mode) {}
};

回答1:


The proper way to do this is to call mActionMode.setTag("1") in onItemCheckedStateChanged and then from the onActionItemClicked function call mode.getTag();




回答2:


Create your own callback by extending the interface ActionMode.Callback

private interface ActionCallback extends ActionMode.Callback {
        public void setClickedView(View view);
}

private ActionCallback mActionModeCallback = new ActionCallback() {

        public View mClickedView;

        public void setClickedView(View view) {
            mClickedView = view;
        }

        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            return true;
        }

        // Called each time the action mode is shown. Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item ) {
            switch ( item.getItemId() ) {
                case R.id.menu_delete:
                    Log.v( TAG, "#onActionItemClicked ready to delete the item with id: " + mClickedView.getTag() );
                    mode.finish();      // Action picked, so close the CAB
                    return true;
                default:
                    return false;
            }   // end switch
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null;
        }
};

For a view which has OnLongClickListener attached, override the onLongClick callback this way.

                        @Override
                // Called when the user long-clicks on someView
                public boolean onLongClick( View view ) {
                    // proceed only when actionmode is not null
                    // otherwise overlapping action modes will be
                    // displayed
                    if( mActionMode != null ) {
                        return false;
                    }

                    mActionModeCallback.setClickedView(view);
                    // Start the CAB using the ActionMode.Callback defined above
                    mActionMode = startActionMode( mActionModeCallback );
                    view.setSelected(true);
                    return true;
                }


来源:https://stackoverflow.com/questions/12939627/passing-id-of-listview-item-to-actionmode-callback-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!