title: Detecting which selected item (in a ListView) spawned the ContextMenu (Android)
body: <p>I have a <a href="http://developer.android.com/reference/android/widget/ListView.html" rel="noreferrer">ListView</a> that will allow the user to long-press an item to get a context menu. The problem I'm having is in determining which <code>ListItem</code> they long-pressed. I've tried doing this:</p>
  
  <pre><code>myListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
    @Override public void onCreateContextMenu(ContextMenu menu, final View v, ContextMenuInfo menuInfo) {
     menu.add("Make Toast")
      .setOnMenuItemClickListener(new OnMenuItemClickListener() {
       @Override public boolean onMenuItemClick(MenuItem item) {
        String toastText = "You clicked position " + ((ListView)v).getSelectedItemPosition();
        Toast.makeText(DisplayScheduleActivity.this, toastText, Toast.LENGTH_SHORT).show();
        return true;
       }
      });
    } 
   });
  </code></pre>
  
  <p>but it just hangs until an <a href="http://en.wikipedia.org/wiki/Application_not_Responding" rel="noreferrer">ANR</a> pops up. I suspect that after the menu is created the <code>ListItem</code> is no longer selected.</p>
  
  <p>It looks like you could monitor for clicks or long-clicks then record the clicked item there:</p>
  
  <pre><code> mArrivalsList.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View v, int position, long id) {
     // record position/id/whatever here
     return false;
    }
   });
  </code></pre>
  
  <p>but that feels majorly kludgey to me. Does anyone have any better solutions for this?</p>