views:

105

answers:

1

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.

A: 

Hi, you can use AdapterContextMenuInfo please refer Link-1 from the code you provided,

public boolean onContextItemSelected(MenuItem **item**) {

if (item.getTitle() == "Edit") {
    **AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    editinfo(info.position);**
} else if (item.getTitle() == "Delete") {
    // delete action
} else {
    return false;
}
return true;

}

Hope this helps you

Vamsi
Great that works but it appears to be offset by one so instead of info.position I am having to pass in (info.position + 1). Is that expected behaviour?