How do I pass the selected item's ID from a ListView to an AlertDialog in Android?
<p>Here's the scenario: I have a ListActivity, and long-pressing on an item brings up a context menu. One item in the context menu is "delete", and that brings up a confirmation box (and AlertDialog). When the user presses OK in the confirmation dialog, I need to know the ID of the item that was originally selected, so that I can actually delete it.</p>

<p>The flow looks like this: </p>

<pre>

      This event:             Causes Android to call:
-----------------------------------------------------  
Long press an item        ->  onCreateContextMenu()
Select context menu item  ->  onContextItemSelected()
call showDialog()         ->  onPrepareDialog()
user clicks OK            ->  onClick()
</pre>

<p>In onCreateContextMenu and onContextMenuSelected, I can get at the id of the selected item from the ContextMenuInfo. In onPrepareDialog, however, I no longer have access to that information. The rub is that onPrepareDialog needs this information to set up an onClick listener on its POSITIVE button.</p>

<p>I know that, during onContextMenuSelected, I can stash the selected item's ID away into a field of my activity. I have done that, and it works. But it's also really ugly. The statefulness that it introduces makes me uneasy. Has anybody else seen a better way to pass such information around than to use fields in the activity?</p>
