identifying id of listitem for contextmenu

Posted by martinjd on Stack Overflow See other posts from Stack Overflow or by martinjd
Published on 2010-05-06T03:21:51Z Indexed on 2010/05/06 3:28 UTC
Read the original article Hit count: 353

Filed under:
|
|

I have a View that extends Activity. A ListView will display a number of listitems. When the user long clicks I would like to present them with a contextmenu allowing them to select edit, delete etc... and then identify the listitem that was selected as the item to perform the action on.

In onCreate I have:

listView.setAdapter(adapter);
listView.setOnItemClickListener(onListClick);
listView.setOnItemLongClickListener(onListLongClick);
registerForContextMenu(listView);

I have a method onCreateContextMenu

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Context Menu");
    menu.add(0, v.getId(), 0, "Edit");
    menu.add(0, v.getId(), 0, "Delete");

}

and also onContextItemSelected

@Override
public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Edit") {
        // edit action
    } else if (item.getTitle() == "Delete") {
        // delete action
    } else {
        return false;
    }
    return true;
}

I am not sure where to go from here to get the correct row/listitem.

© Stack Overflow or respective owner

Related posts about android

Related posts about contextmenu