Why is MenuItem.AdapterContextMenuInfo null when my list view has a custom adapter?
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:
Norm开发者_Go百科ally, 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));
I suspect that your problem is:
My custom Adapter makes each list row a checkbox and a text view
Temporarily get rid of the checkbox. If your context menu now works, then the checkbox is the issue. Consider switching to a CheckedTextView
instead -- that's much better supported with ListView anyway (e.g., use CHOICE_MODE_MULTIPLE
, setItemChecked()
).
I have used context menus with custom adapters and have not had any issues. But, I have not used checkboxes in my rows.
精彩评论