Ansh
Ansh

Reputation: 2396

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

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

Upvotes: 1

Views: 9109

Answers (1)

Maciej Ciemięga
Maciej Ciemięga

Reputation: 10715

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

Upvotes: 6

Related Questions