Passing Id Of Listview Item To Actionmode.callback Object
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
Solution 1:
The proper way to do this is to call mActionMode.setTag("1")
in onItemCheckedStateChanged
and then from the onActionItemClicked
function call mode.getTag();
Solution 2:
Create your own callback by extending the interface ActionMode.Callback
privateinterfaceActionCallbackextendsActionMode.Callback {
publicvoidsetClickedView(View view);
}
privateActionCallback mActionModeCallback = newActionCallback() {
publicView mClickedView;
publicvoidsetClickedView(View view) {
mClickedView = view;
}
// Called when the action mode is created; startActionMode() was called@OverridepublicbooleanonCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu itemsMenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
returntrue;
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but// may be called multiple times if the mode is invalidated.@OverridepublicbooleanonPrepareActionMode(ActionMode mode, Menu menu) {
returnfalse; // Return false if nothing is done
}
// Called when the user selects a contextual menu item@OverridepublicbooleanonActionItemClicked(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 CABreturntrue;
default:
returnfalse;
} // end switch
}
// Called when the user exits the action mode@OverridepublicvoidonDestroyActionMode(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 someViewpublicbooleanonLongClick( View view ) {
// proceed only when actionmode is not null// otherwise overlapping action modes will be// displayedif( mActionMode != null ) {
returnfalse;
}
mActionModeCallback.setClickedView(view);
// Start the CAB using the ActionMode.Callback defined above
mActionMode = startActionMode( mActionModeCallback );
view.setSelected(true);
returntrue;
}
Post a Comment for "Passing Id Of Listview Item To Actionmode.callback Object"