Detecting which selected item (in a ListView) spawned the ContextMenu (Android)

后端 未结 8 833
难免孤独
难免孤独 2020-11-27 12:27

I have a ListView that will allow the user to long-press an item to get a context menu. The problem I\'m having is in determining which ListItem they long-press

8条回答
  •  天命终不由人
    2020-11-27 12:50

    We have used with success:

    @Override
    public boolean onContextItemSelected
    (
    MenuItem item
    )
        {
        if (!AdapterView.AdapterContextMenuInfo.class.isInstance (item.getMenuInfo ()))
            return false;
    
        AdapterView.AdapterContextMenuInfo cmi =
            (AdapterView.AdapterContextMenuInfo) item.getMenuInfo ();
    
        Object o = getListView ().getItemAtPosition (cmi.position);
    
        return true;
        }
    

提交回复
热议问题