问题
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"
回答1:
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) {
回答2:
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
}
回答3:
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);
}
回答4:
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
回答5:
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;
}
来源:https://stackoverflow.com/questions/6146909/oncontextitemselected-doesnt-get-called