12

I have a dialog that shows a list of items, I need to be able to edit/delete items in this list so I put a context menu in so when the user long presses on an item it they can choose what they want to do (edit or delete the item).

The problem is that onContextItemSelected never gets called when an item in the context menu is selected.

I checked to see if maybe the activity that created the dialog fragment is getting the callback but that is not either so why is there it not getting called? Can you not do a context menu in a dialog?

public class TypesDialogList extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor>,OnItemClickListener,OnCreateContextMenuListener{

ListView lv;
SimpleCursorAdapter mAdapter;
private int EDIT_TYPE = 1;
private int DELETE_TYPE = 2;
OnEditType mType;


public Dialog onCreateDialog(Bundle state){
    final View v = getActivity().getLayoutInflater().inflate(R.layout.layer_dialog_layout, null, false);
    lv = (ListView)v.findViewById(R.id.listView1);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setOnItemClickListener(this);
    lv.setOnCreateContextMenuListener(this);
    return new AlertDialog.Builder(getActivity()).setView(v).setPositiveButton("Add Type", new OnClickListener(){

        public void onClick(DialogInterface dialog, int which) {

        }

    }).setTitle("Type's").create();
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    long id = info.id;
    if(id > 3){
        menu.setHeaderTitle("Type Menu");
        menu.add(Menu.NONE, EDIT_TYPE, 1, "Edit");
        menu.add(Menu.NONE, DELETE_TYPE, 2, "Delete");
    }else{
        Toast.makeText(getActivity(),"Cannot edit type",Toast.LENGTH_SHORT).show();
    }

}

@Override
public boolean onContextItemSelected(MenuItem item) {
    super.onContextItemSelected(item);
     AdapterView.AdapterContextMenuInfo oMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
     long id = oMenuInfo.id;
     if(item.getItemId() == EDIT_TYPE){

     }else if(item.getItemId() == DELETE_TYPE){

     }
     return true;
 }

}

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • select the below answer, it is working – Nasrudeen Nov 16 '14 at 05:52
  • Probably the hosting activity has `onContextItemSelected` implementation which does not call `super`. This was the problem in my (somewhat similar) case. – Stan Jan 13 '15 at 22:10

1 Answers1

31

For anybody still looking for a workaround, I just solved this problem by creating an anonymous OnMenuItemClickListener that delegates back to onContextItemSelected(MenuItem item) and setting it on all the items in my menu.

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    // Creation/inflate menu here

    OnMenuItemClickListener listener = new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            onContextItemSelected(item);
            return true;
        }
    };

    for (int i = 0, n = menu.size(); i < n; i++)
        menu.getItem(i).setOnMenuItemClickListener(listener);
}
Raider
  • 740
  • 6
  • 11
  • 1
    Incredible... Is this a bug in the framework or is this really not supported without a workaround like this? – mgalgs Jan 10 '15 at 07:45
  • Either way it's yet another reason for me to move away from Android, I can't use this workaround because my dialog is used as a none dialog fragment in another screen and this would cause 2 invocations – alex.p Jul 23 '15 at 15:35
  • 1
    This can't be true... I just found this, and it solved my issue why onContextItemSelected wasn't being called.. +1 You would think for 3+ years they would fix it. – andyADD May 17 '16 at 09:11