0

This is the onCreate and oncontextitemslected code

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
    Toast toast;
        if(item.getItemId() == R.id.context_menu_edit)
        {
            Log.d("ContextCheck","EDIT!");
            toast = Toast.makeText(this, "Edit!", Toast.LENGTH_SHORT);
            toast.show();
        }

        if(item.getItemId() == R.id.context_menu_delete)
        {
            Log.d("ContextCheck","DELETE!");
            toast = Toast.makeText(this, "Delete!", Toast.LENGTH_SHORT);
            toast.show();
        }

        return super.onContextItemSelected(item);
    }

and before that is i used the method registerForContextMenu(event_list) where event_list is a ListView , no i don't know why when ever i click an item from the context menu, it doesn't do anything, it won't show the toast and won't log into the logcat... is the item.getItemId() same for OptionsMenu and ContextManu?.. i don't know what is wrong with my code..

PS the context menu is called inside a dialog box in a listview

lemoncodes
  • 2,371
  • 11
  • 40
  • 66

2 Answers2

1

Here is your solution, if you don't mind creating the menu items in your class. The keyword was definitely your PS, meaning your listview is in a dialog.

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
              super.onCreateContextMenu(menu, v, menuInfo);
              //MenuInflater inflater = getMenuInflater();
              //inflater.inflate(R.menu.context_menu, menu);

              MenuItem delete = menu.add("delete");
              MenuItem add = menu.add("add");
              add.setIcon(android.R.drawable.ic_menu_upload); //adding icons
              delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                      public boolean onMenuItemClick(MenuItem item) {
                          Log.d("ContextCheck","EDIT!");
                            Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
                              return true;
                      }
              });
              add.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                  public boolean onMenuItemClick(MenuItem item) {
                      Log.d("ContextCheck","EDIT!");
                        Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
                      return true;
                  }
          });
            }

You do not even need the onContextItemSelected method.

erdomester
  • 11,789
  • 32
  • 132
  • 234
  • yep yep it works fine!.. thank you very much!.. but one question why won't it work with the normal implementation of a context on dialog box on listview?... – lemoncodes Aug 12 '12 at 03:26
  • I would say onContextItemSelected can be used for top-level classes, like an activity, and the dialog is a derived class. However according to the dev site, the package names are android.app.Activity and android.app.Dialog, so I don't know. Please delete your first comment, since it's irrelevant now. – erdomester Aug 12 '12 at 10:10
0

You need to return true in the onCreateOptionsMenu as detailed in the documentation:

Returns

You must return true for the menu to be displayed; if you return false it will not be shown.

So you can chacnge your code to this:

@Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            return true;
        }

UPDATE:

I have things return on the options menu with a switch case in a onOptionsItemSelected vs onContextItemSelected

public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.emaildev:    
                    email();
                    break;
                case R.id.share:     
                    Share();
                    break;                      
            }
            return true;
        }

The icons and ids are in my menu.xml

Nick
  • 9,285
  • 33
  • 104
  • 147
  • yep even though i didn't return true, i still got the options on the context menu, but i it won't do anything when i click the items in the context menu by the way how can u return somthing when its void? – lemoncodes Aug 11 '12 at 14:03
  • yes but how are these gonna be implemented on ContextMenu, my options menu are working fine.. its just that on my ContextMenu, i don't do anything when i click an item on the contextmenu – lemoncodes Aug 11 '12 at 14:14
  • See this post: http://stackoverflow.com/questions/6146909/oncontextitemselected-doesnt-get-called , Need to added an int arg to the onContextItemSelected method – Nick Aug 11 '12 at 14:20
  • I don't get it, why you say you need to return true inside a method that is declared as void ? That's not correct and nothing to do with the solution. – Levent Divilioglu Jun 18 '16 at 02:48