English 中文(简体)
未调用onContextItemSelected
原标题:onContextItemSelected doesn t get called

我做了一个简单的应用程序,它只会显示AlertDialog,列表中有四项。我注册了一个上下文菜单。当我长时间单击其中一个项目时,我会得到上下文菜单。然后,我从上下文菜单中选择一个项目,但onContextItemSelected从未被调用。有什么帮助吗?谢谢

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;
    }
}

在main.xml中,我只是用android定义了一个按钮:onClick=“selectScheme”

最佳回答

此行之后:

    registerForContextMenu(alert.getListView());

键入以下内容:

    alert.getListView().setOnCreateContextMenuListener(this);

而不是onContextItemSelected(MenuItem项)函数,请使用以下内容:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem menuItem) {
问题回答

以下函数总是被执行。

 public boolean onMenuItemSelected(int featureId, MenuItem menuItem)

但是,您可以使用以下标志来区分上下文菜单和选项菜单:

if (featureId == Window.FEATURE_CONTEXT_MENU)
{
//Do something
}
else if (featureId == Window.FEATURE_OPTIONS_PANEL)
{
//Do something else
}

只需添加

@Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
    if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
        return onContextItemSelected(aMenuItem);
    else
        return super.onMenuItemSelected(aFeatureId, aMenuItem);
}

我想在米奇的回答基础上再接再厉。

我只使用

registerForContextMenu(this.getListView());

this.getListView().setOnCreateContextMenuListener(this);

在我的父ListActivity类中。

我将代码从onContextItemSelected(MenuItem项)方法移动到了onMenuItemSelected

  @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 h和ler 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 h和led in the super.class.

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

    ...

  } else {
    return super.onMenuItemSelected(featureId, item);
  }
}// end onMenuItemSelected

此问题的真正原因是您重写了onContextItemSelected,而没有调用super.onContextItemSelected。请将onContextItemSelect方法更改为:

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (super.onContextItemSelected(MenuItem item))
        return true;
    return false;
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签