views:

646

answers:

2

Hi,

I have a list view filled with data. I set up a context menu for the listview using the following code:

list.setOnCreateContextMenuListener
(
  new View.OnCreateContextMenuListener() 
  {
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) 
        {
       AdapterContextMenuInfo mi =(AdapterContextMenuInfo) menuInfo;
       menu.add(0, 0, 0, "Delete item");                
    }
   }
);

I have the following method override to control de contextmenu menuitem selected:

@Override
public boolean onContextItemSelected(MenuItem item) 
{
  switch(item.getItemId()) 
  { 
  case 0: 
    ShowAlert("hello from delete item");
    break; 
  default: 
  return super.onContextItemSelected(item); 
  } 
  return true; 
}

In this overrided method, how could i find the item of the list view that was clicked?

Thanks in advance. Best Regards. Jose

+1  A: 
private static final int EDIT_ID = Menu.FIRST + 3;
private static final int DELETE_ID = Menu.FIRST + 4;
 @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit").setAlphabeticShortcut(
                'e');
        menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete")
                .setAlphabeticShortcut('d');
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        switch (item.getItemId()) {
        case EDIT_ID:

            edit(info.id);
            return (true);
        case DELETE_ID:

            delete(info.id);
            return (true);
        }

        return (super.onOptionsItemSelected(item));
    }
Pentium10
Thanks a lot.Best Regards.Jose
Josemalive
+1  A: 

You can use the ContextMenu.ContextMenuInfo.

Something like that:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
}

You can also get the exact View for which the menu is being displayed:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
    View view = info.targetView;
}
Dimitar Dimitrov
Thanks a lot.Best Regards.jose
Josemalive