How to get listitem id when I click on PopUp menu item?

user818455

In my application I have attached popup menu with each item in listview. The popup menu has further two items when we click on popup menu icon.I have implemented OnMenuItemClickListener in my activity to listen for popup menu item clicks which is working fine.But the problem is that How do I get to know the listitem id (not popup menu item id) when I click on popup menu icon for any listview item.The popup menu code is below:

public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(this, v);
        popup.setOnMenuItemClickListener(this);
        popup.inflate(R.menu.actions);
        popup.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_play:
                return true;
            default:
                return false;
        }
    }
Maciej Ciemięga

Please tell me what is "listitem id" that you want to know? I doubt that it's a "listitem view's id". Probably you're thinking about "position", right?

I don't know where do you call showPopup(View v) from, but you also need to pass the position there:

public void showPopup(View v, int listItemPosition) {
    PopupMenu popup = new PopupMenu(this, v);
    popup.setOnMenuItemClickListener(this);
    popup.inflate(R.menu.actions);
    popup.show();
}

Your goal is to know this position in the onMenuItemClick(MenuItem item) callback.
The simplest way to achieve this is to create variable "listItemPositionForPopupMenu", store this position there and read it in the onMenuItemClick callback:

private int listItemPositionForPopupMenu;

public void showPopup(View v, int listItemPosition) {
    listItemPositionForPopupMenu = listItemPosition;
    PopupMenu popup = new PopupMenu(this, v);
    popup.setOnMenuItemClickListener(this);
    popup.inflate(R.menu.actions);
    popup.show();
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_play:
            // read the listItemPositionForPopupMenu here
            return true;
        default:
            return false;
    }
}

You can also do it in many other ways, like creating you own OnMenuItemClickListener listener with listItemPosition variable in constructor and create custom interface with onMenuItemClick(MenuItem item, int listItemPosition). Or you can just create an anonymous class, then you don't need to have the listItemPositionForPopupMenu member variable:

public void showPopup(View v, final int listItemPosition) {
    PopupMenu popup = new PopupMenu(this, v);
    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_play:
                    // read the listItemPosition here
                    return true;
                default:
                    return false;
            }
        }
    });
    popup.inflate(R.menu.actions);
    popup.show();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

espresso long click on menu item and popup menu

From Dev

Overflow Menu on item click for Popup Menu

From Dev

dojo tabContainer - how to get tab id under right-mouse click popup menu for tab not in focus

From Dev

How to get the tree-view item which triggered a popup menu?

From Dev

How to close drawer menu when click on item menu?

From Dev

How to get the id of a toolbar menu item?

From Dev

How can i make my sub-menu show and hide when click on main item?

From Dev

Custom item in context menu when I right-click on folder

From Dev

How can I get menu item in NavigationView?

From Dev

How I get menu click event

From Dev

How I get menu click event

From Dev

How do I change POPUP Text of Menu without ID

From Dev

Open extension popup when click on context menu

From Dev

show popup menu when click on button

From Dev

How I can get list item values when I click on list view

From Dev

Close sidebar menu when click outside of it and when click on menu item

From Dev

how to get single popup info windows when click boxdiv

From Dev

Joomla get menu item id

From Dev

Joomla get menu item id

From Dev

How to set an action when I click outside of the popup window?

From Dev

How to set an action when I click outside of the popup window?

From Dev

ListItem Click event not working when i am putting ListItem's button's click event in android

From Dev

kango how to open popup on click of Context menu

From Dev

How to hide the menu in mobile on click on the menu item?

From Dev

How to get the id or details of the item right clicked from context menu?

From Dev

How do I get the id of a list item?

From Dev

Get the position of a recyclerview item when I click on an imagebutton in another activity

From Dev

Dropdown menu item hide when click away?

From Dev

Dropdown menu item hide when click away?

Related Related

  1. 1

    espresso long click on menu item and popup menu

  2. 2

    Overflow Menu on item click for Popup Menu

  3. 3

    dojo tabContainer - how to get tab id under right-mouse click popup menu for tab not in focus

  4. 4

    How to get the tree-view item which triggered a popup menu?

  5. 5

    How to close drawer menu when click on item menu?

  6. 6

    How to get the id of a toolbar menu item?

  7. 7

    How can i make my sub-menu show and hide when click on main item?

  8. 8

    Custom item in context menu when I right-click on folder

  9. 9

    How can I get menu item in NavigationView?

  10. 10

    How I get menu click event

  11. 11

    How I get menu click event

  12. 12

    How do I change POPUP Text of Menu without ID

  13. 13

    Open extension popup when click on context menu

  14. 14

    show popup menu when click on button

  15. 15

    How I can get list item values when I click on list view

  16. 16

    Close sidebar menu when click outside of it and when click on menu item

  17. 17

    how to get single popup info windows when click boxdiv

  18. 18

    Joomla get menu item id

  19. 19

    Joomla get menu item id

  20. 20

    How to set an action when I click outside of the popup window?

  21. 21

    How to set an action when I click outside of the popup window?

  22. 22

    ListItem Click event not working when i am putting ListItem's button's click event in android

  23. 23

    kango how to open popup on click of Context menu

  24. 24

    How to hide the menu in mobile on click on the menu item?

  25. 25

    How to get the id or details of the item right clicked from context menu?

  26. 26

    How do I get the id of a list item?

  27. 27

    Get the position of a recyclerview item when I click on an imagebutton in another activity

  28. 28

    Dropdown menu item hide when click away?

  29. 29

    Dropdown menu item hide when click away?

HotTag

Archive