如何在 Android 上的 ListActivity 中实现上下文菜单?

发布于 2024-07-12 03:38:38 字数 62 浏览 3 评论 0原文

如何实现通过长按或点击使用内置布局和 ListAdapter 的 ListActivity 触发的上下文菜单?

How do you implement a context menu triggered by a long click or tap on a ListActivity that is using the built in layouts and a ListAdapter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

姜生凉生 2024-07-19 03:38:38

在 onCreate 方法上调用 registerForContextMenu像这样:

registerForContextMenu(getListView());

然后填充菜单 onCreateContextMenu(ContextMenu 菜单,View 视图,ContextMenuInfo menuInfo)。 menuInfo 参数可以通过这种方式提供有关哪个项目被长按的信息:

AdapterView.AdapterContextMenuInfo info;
try {
    info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
    Log.e(TAG, "bad menuInfo", e);
    return;
}
long id = getListAdapter().getItemId(info.position);

并且您可以以通常的方式调用 menu.add

menu.add(0, MENU_ITEM_ID, 0, R.string.menu_string);

当用户选择一个选项时,onContextItemSelected 被调用。 还有 onMenuItemSelected 和文档中没有明确解释这一事实,只是说您使用其他方法从上下文菜单接收调用; 请注意,不要共享 ID。

在 onContextItemSelected 上,您可以通过调用 getMenuInfo()

try {
    info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
    Log.e(TAG, "bad menuInfo", e);
    return false;
}
long id = getListAdapter().getItemId(info.position);

On the onCreate method call registerForContextMenu like this:

registerForContextMenu(getListView());

and then populate the menu on onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo). The menuInfo argument can provide information about which item was long-clicked in this way:

AdapterView.AdapterContextMenuInfo info;
try {
    info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
    Log.e(TAG, "bad menuInfo", e);
    return;
}
long id = getListAdapter().getItemId(info.position);

and you add menu items in the usual way calling menu.add:

menu.add(0, MENU_ITEM_ID, 0, R.string.menu_string);

and when the user picks an option, onContextItemSelected is called. Also onMenuItemSelected and this fact is not explicitly explained in the documentation except to say that you use the other method to receive the calls from the context menu; just be aware, don't share ids.

On onContextItemSelected you can get ahold of the MenuInfo and thus the id of the item selected by calling getMenuInfo():

try {
    info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
    Log.e(TAG, "bad menuInfo", e);
    return false;
}
long id = getListAdapter().getItemId(info.position);
似最初 2024-07-19 03:38:38
listView = (ListView) findViewById(R.id.listpockets);
registerForContextMenu(listView);



public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) {
    //AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(getString(R.string.titleDelete));   
    menu.add(0, CommonUtil.CONTEXT_MENU__DELETE_ID, 0, getString(R.string.menuDelete));
};
@Override
public boolean onContextItemSelected(MenuItem item) {

    if(item.getItemId() == CommonUtil.CONTEXT_MENU__DELETE_ID)
    {
       AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
       long id = this.listView.getItemIdAtPosition(info.position);
       Log.d(TAG, "Item ID at POSITION:"+id);
    }
    else
    {
        return false;
    }
    return true;
}
listView = (ListView) findViewById(R.id.listpockets);
registerForContextMenu(listView);



public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) {
    //AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(getString(R.string.titleDelete));   
    menu.add(0, CommonUtil.CONTEXT_MENU__DELETE_ID, 0, getString(R.string.menuDelete));
};
@Override
public boolean onContextItemSelected(MenuItem item) {

    if(item.getItemId() == CommonUtil.CONTEXT_MENU__DELETE_ID)
    {
       AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
       long id = this.listView.getItemIdAtPosition(info.position);
       Log.d(TAG, "Item ID at POSITION:"+id);
    }
    else
    {
        return false;
    }
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文