3

I have made a simple app that just brings up an AlertDialog, with four items in the list. I register a context menu. When I long click one of the items, I get the context menu. I then select an item from the context menu, but onContextItemSelected never gets called. Any help? Thanks.

test.java:

package com.cerulean.tech.creations.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class test extends Activity {

    private String[] files;
    AlertDialog alert;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        files = new String[4];
    }

    public void selectScheme(View v) {
        files[0] = "<New Scheme>";
        files[1] = "test1";
        files[2] = "test2";
        files[3] = "test3";
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setItems(files, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int item) {
                   }});  
        alert = builder.create();
        alert.show();
        registerForContextMenu(alert.getListView());
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Context Menu");
        menu.add(0, v.getId(), 0, "Delete");
        menu.add(0, v.getId(), 0, "Cancel");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return false;
    }
}

In main.xml, I just a button defined with android:onClick="selectScheme"

mattprecious
  • 302
  • 3
  • 15
DNewell
  • 475
  • 2
  • 8
  • 16

5 Answers5

10

After this line:

    registerForContextMenu(alert.getListView());

type this:

    alert.getListView().setOnCreateContextMenuListener(this);

And instead of onContextItemSelected(MenuItem item) function use this:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem menuItem) {
Miki
  • 440
  • 4
  • 7
  • I just want to say that I have spent a long time on this issue, and your answer is the ONLY one that has worked. I first tried the other 20 or so possible solutions first. For those that might not understand, yes, move the code from onContextItemSelected to the onMenuItemSelected method. And then delete the onContextItemSelected method. – Bryan Dec 12 '13 at 07:59
4

The following function always gets executed.

 public boolean onMenuItemSelected(int featureId, MenuItem menuItem)

But you can differentiate between context menus and option menus using the following flags:

if (featureId == Window.FEATURE_CONTEXT_MENU)
{
//Do something
}
else if (featureId == Window.FEATURE_OPTIONS_PANEL)
{
//Do something else
}
Vikram
  • 11,885
  • 5
  • 22
  • 16
1

Just Add

@Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
    if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
        return onContextItemSelected(aMenuItem);
    else
        return super.onMenuItemSelected(aFeatureId, aMenuItem);
}
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
0

I want to build on Miki's answer.

I only used

registerForContextMenu(this.getListView());

and

this.getListView().setOnCreateContextMenuListener(this);

in my parent ListActivity class.

I moved my code from the onContextItemSelected(MenuItem item) method to the onMenuItemSelected method.

  @Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
    try {
      if (item.getItemId() == R.id.new_entry_menu_option) {

        ...

      }

      ...

      if (item.getItemId() == R.id.quit) {
        /* perform cleanup */
        ...
        return true;
      } else if (item.getItemId() == R.id.delete_entry_context_menu_option) {
        displayConfirmRequest(DELETE_CONFIRMATION_MESSAGE, item);
        return true;
      } else if (item.getItemId() == R.id.2ND_CONTEXT_OPTION) {
        //code for 2nd option
        return true;
      } else if (item.getItemId() == R.id.3RD_CONTEXT_OPTION) {
        //code for 3RD option
        return true;
      } else {
        return super.onMenuItemSelected(featureId, item);
      }// end if/else if/else
    }// end try
    catch (Exception error) {
      //error handler code
      return false;
    }// end try/catch
  }// end onMenuItemSelected

And make sure in your subclass, if you override onMenuItemSelected in the subclass of your super.ListActivity class, to include the following code if you want the contextmenu options to be handled in the super.class.

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
  if (<condition>) {

    ...

  } else {
    return super.onMenuItemSelected(featureId, item);
  }
}// end onMenuItemSelected
Bryan
  • 3,629
  • 2
  • 28
  • 27
-1

The real cause of this problem is that you're overriding onContextItemSelected and not calling super.onContextItemSelected. Change your onContextItemSelected method to this:

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (super.onContextItemSelected(MenuItem item))
        return true;
    return false;
}
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Dan Goldstein
  • 23,436
  • 10
  • 47
  • 51