I\'ve several buttons registered for context menu
how do I know which button was clicked for the menu to appear?
below is the pseudocode that i\'ll be using.
I think it makes more sense to use the ID of the specific view. Say you've got an ListView populated of items containing your data, but in-between some of the items you've created separators/headers. You don't want the separators to handle clicks/long clicks.
In some cases it's totally fine to just refer to "position" or MenuInfo.id, but depending on your data structure you might need more control.
What you can do is to set ID's for the items/views within your ListView (view.setId(x), where x represents the ID/position for your data structure/object. Then, when creating a ContextMenu and handling selections within it do the following to read that ID out:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
int id = info.targetView.getId();
// now you can refer to your data with the correct ID of yours
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int id = info.targetView.getId();
// now you can refer to your data with the correct ID of yours
}