android - onCreateContextMenu 被 ContextMenuInfo 的 null 值调用

标签 android listview android-cursor

我正在尝试使用上下文菜单。我已经使用 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(...) 方法中?

最佳答案

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

问题

  • 在行布局中使用 CheckBox 会影响上下文菜单的使用
  • 我认为,Che​​ckedTextView 旨在用于多选,它不适合初始化选中状态。

我采用的解决方案做了以下事情

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

关于android - onCreateContextMenu 被 ContextMenuInfo 的 null 值调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1718765/

相关文章:

android - 如何从主机名中提取网站名称

android - 更改 ExpandableListView 中的可扩展指示器

android - Fragment 中的 ListView 未显示在 MainActivity 中

android - 如何将 2 个游标表示为 1 个已排序的游标?

java - 如何在安卓平板电脑的屏幕上显示鼠标光标?

android - cursor.getString() 是否将 int 强制转换为字符串?

java - 将 Activity 类更改为 Fragment

android - 使用 ViewPager 的 CollapsingToolbarLayout

android - PopupWindow 中 ListView 的问题

c# - 读取十六进制文件,转换为字符串并加载到列表框