Development Resource Project
JQuery Venetian Blinds Transition Effect
Changing Mailman Python Scripts for Virtual Host Support
Getting Set up with Ogre 3D on Ubuntu
A Simple ISAPI Filter for Authentication on IIS
Enforce Coding Standards with PHP_CodeSniffer and Eclipse IDE on Ubuntu Linux

Accessing ListView Items with a Context Menu

Sunday, 2 October 11, 1:00 pm
The first thing to do is to register your ListView as having a context menu, using the registerForContextMenu() method. The following lines of code in your ListActivity's onCreate() method accomplish this:
ListView lv = getListView(); registerForContextMenu(lv);
Next we need to set up the menu options that the user will be able to choose from. It can be useful to define a suitably named class constant for each of our options:
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.

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Repair Video