I'm using Contextual Action Barwith ListView (CHOICE MODE SINGLE). Everything works but I don't know how to retrieve the selected item.
listViewData.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listViewData.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mActionMode != null) {
return false;
}
mActionMode = ((ActionBarActivity) getActivity()).startSupportActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
mode.finish();
return true;
case R.id.menu_share:
mode.finish();
return true;
case R.id.menu_delete:
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
you can use
ActionMode.setTag()
in onItemLongClick to pass selected item position toonActionItemClicked
usingActionMode.getTag()
as :and set selected item in onActionItemClicked :