How to show ContextMenu long click on listview android?

前端 未结 2 442
不思量自难忘°
不思量自难忘° 2021-01-22 03:40

here is a list of contact in ListView , i want when user longClick on any contacts then ContextMenu popup should be show \"call\"and \"sen

2条回答
  •  -上瘾入骨i
    2021-01-22 04:16

    Try to swap the lines from:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
      menu.setHeaderTitle("Select The Action");
      menu.add(0, v.getId(), 0, "Call");
      menu.add(0, v.getId(), 0, "Send SMS");
    }
    

    To:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
      menu.setHeaderTitle("Select The Action");
      menu.add(0, v.getId(), 0, "Call");
      menu.add(0, v.getId(), 0, "Send SMS");
      super.onCreateContextMenu(menu, v, menuInfo);
    }
    

    Update

    Remove listView.setOnItemLongClickListener because you can't use a longclicklistener and a context menu at the same time. To show a context menu you just need to call registerForContextMenu(listView) after you inflated your view and override onCreateContextMenu() for menu creation and onContextItemSelected() to handle the user action.

提交回复
热议问题