14

I'm trying to create a context menu that changes the available options depending on which row was clicked.

But I can't figure out how to get the row position in the listview for row that was clicked, until after the menu is created.

Ray Britton
  • 8,257
  • 3
  • 23
  • 32

2 Answers2

48

It is not obvious at all, but here's the code:

   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
   {    
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        int position = info.position;
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • @TusharAgarwal I'm facing similar problem, have you found any alternative – Vinay Bhargav Jul 17 '14 at 11:26
  • @VinayBhargav Have you registered for contextMenu ? like .. `registerForContextMenu(getListView());` – Tushar Agarwal Jul 18 '14 at 06:59
  • @TusharAgarwal yeah .. i'm using gridview .. I've gone through many posts .. nothing helped me :| – Vinay Bhargav Jul 18 '14 at 07:32
  • I've got it working finally, I was using custom GridView in which `getContextMenuInfo()` method was not implemented. I've posted [here](http://vinaybhargav.wordpress.com/2014/07/20/android-floating-context-menu-for-listviewgridview/) in case if someone need a sample. – Vinay Bhargav Jul 20 '14 at 08:35
0

The above solution did not work for me. Here is what I used:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0, v.getId(), 0, context.getString(R.string.tabTitleEnrolment));
        menu.add(0, v.getId(), 0, context.getString(R.string.tabTitleAfterCare));
        menu.add(0, v.getId(), 0, context.getString(R.string.contextMenuDelete));

        pos = getPosition();
    }
makunomark
  • 789
  • 6
  • 10