【问题标题】:context menu not working上下文菜单不起作用
【发布时间】:2015-08-24 00:55:54
【问题描述】:

大家好,我正在查看 Android 市场上的“Diaro”和“我的日记”应用程序。 这些项目显示在列表视图中,长按一个项目,一个带有各种选项(如编辑、删除等)的上下文菜单打开。我尝试在我的应用程序中实现相同的功能,这有些相似。但问题出在onContextItemSelected(MenuItem item) 我无法获得点击项目的内容。这是onContextItemSelected(MenuItem item)的代码:

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    switch (item.getItemId()) {
    case R.id.edit:


        break;v

          // rest of the codetion
    }

    return super.onContextItemSelected(item);
}

有人能告诉我如何从这个函数中获取在列表视图上单击的项目的 ID 吗?我真的可以在这里使用一些帮助:)

【问题讨论】:

    标签: android


    【解决方案1】:

    您必须为 contextMenu 注册 yourView,如下所示:

    list = getListView();
    registerForContextMenu(list);
    

    你必须使用 onCreateContextMenu 来构建它

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu,
                                    View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo info =
                (AdapterView.AdapterContextMenuInfo) menuInfo;
        selectedWord = ((TextView) info.targetView).getText().toString();
        selectedWordId = info.id;
    
        contextMenu.setHeaderTitle(selectedWord);
        contextMenu.add(0, CONTEXT_MENU_EDIT_ITEM, 0, R.string.edit);
        contextMenu.add(0, CONTEXT_MENU_DELETE_ITEM, 1, R.string.delete);
    }
    

    您的 contextMenu 标题中有 listView 项目,它的 id 在 selectedWordId 中

    更多信息请参见此链接:Detecting which selected item (in a ListView) spawned the ContextMenu (Android)

    【讨论】:

    • 我没有问题膨胀菜单@Alish,问题是这个。问题是,如果用户点击上下文菜单中的编辑选项,用户将如何获得项目的rowId被public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 函数点击
    • 我所说的 id 是指列表视图中项目的 id,
    • 数据库中有数据吗?还是?
    【解决方案2】:

    我有一段时间遇到同样的问题,发现我的活动中有 onMenuitemselected(),这是在听上下文菜单项而不是 contextitemselected(),希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多