简体   繁体   中英

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?

@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 (eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM