I have parsed through a lot of the posts here and haven't found anything quite like my problem.
Basically I am trying to call openContextMenu(l)
in onListItemClick
. Doing so creates a context menu with no menuInfo
. Performing a long click will work correctly. After the long click is performed, my code will start working and actually get a menuInfo
that is not null.
I have a ListActivity
that is filled with a SimpleCursorAdapter
which grabs data from SQL
.
In my onCreate I registerForContextMenu(getListView())
.
I have also tried using registerForContextMenu(l)
just before the openContextMenu(l)
call.
Any help would be appreciated! Thanks.
Here is a sample of my code:
public class MY_Activity extends ListActivity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
UpdateTable();
registerForContextMenu(getListView());
}
...
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
super.onListItemClick(l, view, position, id);
//THIS DOESNT WORK UNLESS A LONG CLICK HAPPENS FIRST
//registerForContextMenu(l); //Tried doing it here too
openContextMenu(l);
//unregisterForContextMenu(l); //Then unregistering here...
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//menuInfo will be null here.
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "One");
menu.add(0, v.getId(), 0, "Two");
menu.add(0, v.getId(), 0, "Three");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(info == null) {
Log.e("","NULL context menu intem info...");
return false;
}
}
public void UpdateTable() {
cursor = DatabaseHelper_Main.GetCursor(my_id);
cursorAdapter = new SimpleCursorAdapter(this, R.layout.my_listview_entry,
cursor, fields, fieldResources, 0);
setListAdapter(cursorAdapter);
}
...
I had a very similar issue today, and the fix was unexpectedly simple but I don't understand why, but I'll post it here anyway.
I use the m_contextMenu boolean to indicate that the context menu is being shown, I have an onItemLongClickListener that returns false if m_contextMenu is true so that the context menu does show up (if onItemLongClick() returns true, it won't show the context menu).