I have been developing Android application which has 3 ListView and one ContextMenu for each view:
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
menu.add(Menu.NONE, CONTEXT_MENU_ITEM_DELETE, Menu.NONE, "Delete");
super.onCreateContextMenu(menu, view, info);
}
Registration for click catching:
this.registerForContextMenu(mFirstCategory);
this.registerForContextMenu(mSecondCategory);
this.registerForContextMenu(mMainCategory);
mFirstCategory, mSecondCategory, mMainCategory are ListViews. Also I have method for getting row clicked:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info=(AdapterContextMenuInfo)item.getMenuInfo();
String name = null;
switch (info.targetView.getId()) {
case (R.id.listViewFirst): name="First";
case (R.id.listViewSecond): name="Second";
case (R.id.listViewMain): name="Main";
}
Toast.makeText(this, name+"_"+String.valueOf(info.position), Toast.LENGTH_LONG).show();
return super.onContextItemSelected(item);
}
I need to define position (row) clicked of ListView and ListView clicked. My “switch/case” block doesn’t work. Please, tell me, how can I do my need?
targetView is the single row from your ListView. It’s parent will be your ListView:
And then do: