I'm attempting to use a ContextMenu. I've successfully done this for a simple ListActivity using SimpleCursorAdapter.
Moving on I want to replace SimpleCursorAdapter with CursorAdapter but still retain the ContextMenu behaviour, so I've added the two mandatory override functions bindView and newView
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.check_row, parent, false);
registerForContextMenu(view);
return view;
}
Note the registerForContextMenu which replaces the registerForContextMenu(getListView()) in the onCreate method of the ListActivity. I found this necessary to get a call to onCreateContextMenu(...)
All this works (rows created with the expected widgets, callbacks on them working etc etc) except the ContextMenuInfo parameter supplied to onCreateContextMenu(...) is now null - stopping me from accessing the rowId.
Is there another trick to perfo开发者_Go百科rm - perhaps in the bindView(...) method of the CursorAdapter?
I'm answering the question - but I would point out that 'commonsware.com' provided the clues and direction, see above.
The problem
- Using CheckBox in the row layout impacts upon the use of Context menus
- CheckedTextView is, I believe, intended for multi-selection, it doesn't lend itself to initialization of the checked state.
The solution I adopted does the following
- Use CheckedTextView
- Extend from CursorAdapter to initialize the checked state during bindView(...)
Note: This must manage displaying the correct icons too - Manage the CheckedTextView's state in onListItemClick(...) and record it in the dBase, not forgetting to update the cursor.
That's not the right approach. You register the context menu for the ListView
, not the rows. Call registerForContextMenu()
up front, perhaps in onCreate()
after you inflate the layout.
精彩评论