listview长按弹出菜单并响应菜单事件

listview长按弹出菜单并响应菜单事件


//设置长按菜单项
listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            public void onCreateContextMenu(ContextMenu menu, View v,
                                            ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("ContextMenu");
                menu.add(0, 0, 0, "menuItem1");
                menu.add(0, 1, 0, "menuItem2");
            }
        });
        upgradeRootPermission(getPackageCodePath());


    public int MID;
//重写Activity中的onContextItemSelected
    public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        MID = (int) info.id;// 这里的info.id对应的就是数据库中_id的值

        switch (item.getItemId()) {
            case 0:

                Toast.makeText(ListExplorer.this,
                        "menuItem1",
                        Toast.LENGTH_SHORT).show();
                break;

            case 1:
                
                Toast.makeText(ListExplorer.this,
                        "menuItem2",
                        Toast.LENGTH_SHORT).show();
                break;


            default:
                break;
        }

        return super.onContextItemSelected(item);

    }

你可能感兴趣的:(listview长按弹出菜单并响应菜单事件)