Why is MenuItem.AdapterContextMenuInfo null when my list view has a custom adapter?

Posted by gregm on Stack Overflow See other posts from Stack Overflow or by gregm
Published on 2010-05-25T11:36:57Z Indexed on 2010/05/25 11:41 UTC
Read the original article Hit count: 150

Filed under:

My question: Before I go and use an OnLongClickListener, is there a better way to pass the "what was clicked to create this context menu" information when your list view has a custom adapter?

Here are some details:

Normally, my code can just do something like this:

public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

and then go on and be happy.

However, ever since I introduced a custom adapter, item.getMenuInfo() is null. This is a big problem, because my code no longer knows which item was clicked. (My custom Adapter makes each list row a checkbox and a text view)

I tried this but failed: Created my own special AdapterContextMenuInfo (called "HasAViewMenuInfo"), but when I pass it in this method, it ends up being null in the menu

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) 
{
    super.onCreateContextMenu(menu, v, new HasAViewMenuInfo(v));

© Stack Overflow or respective owner

Related posts about android