使用 ContextMenuInfo 的 null 值调用 onCreateContextMenu
我正在尝试使用上下文菜单。我已经使用 SimpleCursorAdapter 成功地为一个简单的 ListActivity 完成了此操作。
继续,我想用 CursorAdapter 替换 SimpleCursorAdapter,但仍保留 ContextMenu 行为,因此我添加了两个强制覆盖函数 bindView 和 newView
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.check_row, parent, false);
registerForContextMenu(view);
return view;
}
请注意,registerForContextMenu 替换了 ListActivity 的 onCreate 方法中的 registerForContextMenu(getListView()) 。我发现有必要调用 onCreateContextMenu(...)
所有这些都有效(使用预期的小部件创建的行,它们的回调工作等),除了提供给 onCreateContextMenu(...) 的 ContextMenuInfo 参数现在为 null - 停止我无法访问 rowId。
是否还有另一个技巧可以执行 - 也许是在 CursorAdapter 的 bindView(...) 方法中?
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 perform - perhaps in the bindView(...) method of the CursorAdapter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在回答这个问题 - 但我想指出“commonsware.com”提供了线索和方向,见上文。
问题
我采用的解决方案执行以下操作:
注意:这也必须管理显示正确的图标。
I'm answering the question - but I would point out that 'commonsware.com' provided the clues and direction, see above.
The problem
The solution I adopted does the following
Note: This must manage displaying the correct icons too
这不是正确的做法。您为
ListView
注册上下文菜单,而不是行。预先调用registerForContextMenu()
,也许在膨胀布局之后在onCreate()
中调用。That's not the right approach. You register the context menu for the
ListView
, not the rows. CallregisterForContextMenu()
up front, perhaps inonCreate()
after you inflate the layout.