Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid listview - on long click shows wrong contextmenu
    primarykey
    data
    text
    <p>I have two listviews, bookmarklistview and historylistview, one on top of another. Bookmarklistview is hidden when historylistview is shown and vice-versa. And for long click event, am creating a contextmenu. The long click event works fine with historylistview.</p> <p>Now problem shows up when I long click an item in bookmarklistview, the historylistview context menu is shown instead of bookmarklistview context menu.</p> <p>How can this happen? Any idea?</p> <p>Below is the code for bookmarklistview contextmenu</p> <pre><code>public void onCreateContextMenuBookmark(ContextMenu menu, View v, ContextMenuInfo menuInfo){ super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Delete Bookmark?"); menu.add(1, 1, 1, "Delete"); menu.add(1, 2, 2, "Cancel"); } public boolean onContextItemSelectedBookmark(MenuItem item){ AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); if(item.getTitle() == "Delete"){ // delete item }else if(item.getTitle() == "Cancel"){ } return false; } </code></pre> <p>Below is the code for historylistview contextmenu</p> <pre><code>public void onCreateContextMenuHistory(ContextMenu menu, View v, ContextMenuInfo menuInfo){ super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Delete History?"); menu.add(1, 1, 1, "Delete"); menu.add(1, 2, 2, "Cancel"); } @Override public boolean onContextItemSelected(MenuItem item){ AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); if(item.getTitle() == "Delete"){ // delete item }else if(item.getTitle() == "Cancel"){ } return false; } </code></pre> <p>Ok, I figured out that I can have a single context menu for both the views, something like this</p> <pre><code> public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); switch(v.getId()){ case R.id.history_listView: menu.setHeaderTitle("Delete History?"); menu.add(0, v.getId(), 0, "Delete"); menu.add(0, v.getId(), 0, "Cancel"); break; case R.id.bookmark_listView: menu.setHeaderTitle("Delete Bookmark?"); menu.add(0, v.getId(), 0, "Delete"); menu.add(0, v.getId(), 0, "Cancel"); } } </code></pre> <p>But how do I call two different onContextItemSelected() methods one for each view?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload