使用ContextMenuInfo的空值调用onCreateContextMenu

Gra*_*y-M 5 android listview cursor

我正在尝试使用ContextMenu。我已经使用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;
}
Run Code Online (Sandbox Code Playgroud)

请注意registerForContextMenu,它取代了ListActivity的onCreate方法中的registerForContextMenu(getListView())。我发现有必要调用onCreateContextMenu(...)

除提供给onCreateContextMenu(...)的ContextMenuInfo参数外,所有这些工作(使用预期的小部件创建的行,其上的回调正常工作等)现在为空-阻止我访问rowId。

还有另一个技巧可以执行-也许在CursorAdapter的bindView(...)方法中?

Gra*_*y-M 2

我正在回答这个问题 - 但我想指出“commonsware.com”提供了线索和方向,见上文。

问题

  • 在行布局中使用复选框会影响上下文菜单的使用
  • 我相信 CheckedTextView 旨在用于多重选择,它不适合初始化选中状态。

我采用的解决方案执行以下操作

  1. 使用CheckedTextView
  2. 从 CursorAdapter 扩展以在 bindView(...) 期间初始化选中状态
    注意:这也必须管理显示正确的图标
  3. 在 onListItemClick(...) 中管理 CheckedTextView 的状态并将其记录在 dBase 中,不要忘记更新光标。