cody
cody

Reputation: 6941

How do I find out which grid row was clicked for contextmenu?

I've got a custom layout which is like a grid. I registered it for contextmenu. Now if I do a long click on it, I'd like to know the position where the user clicked on. I have to let my custom layout implement MenuInfo right? But what about the click position?

Upvotes: 1

Views: 1041

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006944

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

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

    return(super.onOptionsItemSelected(item));
}

In onContextItemSelected(), if the context menu is for an AdapterView (e.g., GridView), you can cast the item.getMenuInfo() object to an AdapterView.AdapterContextMenuInfo object. That object has an id and a position field. The id is the _ID if you are using a CursorAdapter. The position is the index into your adapter.

Upvotes: 1

Related Questions