上下文菜单不使用可扩展列表视图项

时间:2013-03-07 08:51:23

标签: android contextmenu expandablelistview

我想在android中为可扩展列表子项实现上下文菜单。我使用以下代码 -

mExpandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view,
                    int arg2, long arg3) {

                int type =ExpandableListView.getPackedPositionType(arg3);

                int groupPos = ExpandableListView.getPackedPositionGroup(arg3);
                int childPos = ExpandableListView.getPackedPositionChild(arg3);

                if(type==ExpandableListView.PACKED_POSITION_TYPE_CHILD){
                    registerForContextMenu(getExpandableListView().getChildAt(arg2));
                    Toast.makeText(getApplicationContext(), "Hello Hello"+"  "+groupPos+" "+childPos, 0).show();
                }
                return true;
            }
        });

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);

        ExpandableListView.ExpandableListContextMenuInfo info =
            (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;

        int type =
            ExpandableListView.getPackedPositionType(info.packedPosition);

        int group =
            ExpandableListView.getPackedPositionGroup(info.packedPosition);

        int child =
            ExpandableListView.getPackedPositionChild(info.packedPosition);


        menu.setHeaderTitle("CCM");
        menu.setHeaderIcon(R.drawable.ic_launcher);
        menu.add(0, v.getId(), 0, "HelloCCM");      

    }

但根本没有上下文菜单。

请帮我弄明白

1 个答案:

答案 0 :(得分:0)

你添加了

吗?
registerForContextMenu(mExpandableListView);

初始化视图时?