Android: How to find the position clicked from the context menu

前端 未结 3 1140
星月不相逢
星月不相逢 2020-12-04 21:50

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.OnCr         


        
3条回答
  •  北海茫月
    2020-12-04 22:38

    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;
    }
    

提交回复
热议问题