2013-03-07 68 views
1

我想在android中實現可展開列表子項的上下文菜單。我正在使用下面的代碼 -上下文菜單不能使用Expandable列表視圖項

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");  

    } 

但沒有任何上下文菜單是打開的。

請幫我找出

回答

0

你加

registerForContextMenu(mExpandableListView); 

初始化視圖時?