protected static final int CONTEXTMENU_OPTION1 = 1;
protected static final int CONTEXTMENU_OPTION2 = 2;
protected static final int CONTEXTMENU_OPTION3 = 3
Items are added and other initialisation tasks (such as setting a title for the menu) are performed by overriding Activity's onCreateContextMenu() method.@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Set title for the context menu
menu.setHeaderTitle("History");
// Add all the menu options
menu.add(Menu.NONE, CONTEXTMENU_OPTION1, 0, "Option One");
menu.add(Menu.NONE, CONTEXTMENU_OPTION2, 1, "Option Two");
menu.add(Menu.NONE, CONTEXTMENU_OPTION3, 2, "Option Three");
}
The important part here is the ContextMenu.add() method for adding context menu items. The first argument to this method is an integer representing the group the menu item belongs to. Use Menu.NONE if it belongs to no group. The second argument is an integer ID for the menu item itself, which we're free to choose ourselves. Normally you'll want a unique ID in order to determine which option was selected later on. The third argument is the sort order: lower numbers appear higher up in the context menu. The final string argument is the label for the menu item.Now for the last thing: taking care of the actions that should be performed when an option is selected from the context menu. We do this by overriding Activity's onContextItemSelected() method.
@Override
public boolean onContextItemSelected(MenuItem item) {
// Get extra info about list item that was long-pressed
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
// Perform action according to selected item from context menu
switch (item.getItemId()) {
case CONTEXTMENU_OPTION1:
// Show message
Toast.makeText(getApplicationContext(), "Option 1: ID "+menuInfo.id+", position "+menuInfo.position, Toast.LENGTH_SHORT).show();
break;
case CONTEXTMENU_OPTION2:
// Show message
Toast.makeText(getApplicationContext(), "Option 2: ID "+menuInfo.id+", position "+menuInfo.position, Toast.LENGTH_SHORT).show();
break;
case CONTEXTMENU_OPTION3:
// Show message
Toast.makeText(getApplicationContext(), "Option 3: ID "+menuInfo.id+", position "+menuInfo.position, Toast.LENGTH_SHORT).show();
break;
}
return true;
}
The getItemId() method of the MenuItem object passed into this method returns the ID of the selected context menu item as set in the call to ContextMenu.add() previously.The AdapterContextMenuInfo object returned by the MenuItem's getMenuInfo() method gives us extra information relating to the item in the ListView that the user long-pressed on when they activated the context menu. We can get the ListItem's id and position this way.
If your ListView's content was created by attaching it to a database cursor, the ID property of the AdapterContextMenuInfo object is the database ID corresponding to the ListItem.
The position property of the AdapterContextMenuInfo object is the ListItem's position in the ListView, which corresponds to the array offset if you created the ListView by binding to an ArrayAdapter.