0

I want to open the aboutActivity upon selecting the contextmenu item but nothing happens when I select any item from the contextmenu.

Here is the code:

public boolean onContextItemSelected(MenuItem menu){
        // get menu id
        int menuid = menu.getItemId();
        switch (menuid){
        case 0:
            Intent intnt2 = new Intent(ListActivity.this,aboutActivity.class);
            startActivity(intnt2);
        break;
        case 1:
            Intent intnt3 = new Intent(ListActivity.this,aboutActivity.class);
            startActivity(intnt3);
        break;
        }

        return super.onContextItemSelected(menu);
    }

I have declared the aboutActivity in manifest file and I have another menu item(named "About") which lauches the aboutActivity.Any help?

[Edit] This is the onCreateContextMenu function:

public void onCreateContextMenu(ContextMenu menu,View view,ContextMenuInfo menuInfo){
        //checking if the long press is coming from ListView or not
        if(view.getId()==R.id.mainlist){
            //AdapterView.AdapterContextMenuInfo ainfo= (AdapterContextMenuInfo) menuInfo;
            menu.setHeaderTitle("Item menu");
            menu.add(Menu.NONE,0,0,"Edit");
            menu.add(Menu.NONE,1,0,"Delete");
        }
    }

How to use R.id here,as raghunandan suggested?

Rajat Saxena
  • 3,834
  • 5
  • 45
  • 63

2 Answers2

3

It should be

      switch (menuid){
      case R.id.id1:
        Intent intnt2 = new Intent(ListActivity.this,aboutActivity.class);
        startActivity(intnt2);
        return true;
       case R.id.id2:
        Intent intnt3 = new Intent(ListActivity.this,aboutActivity.class);
        startActivity(intnt3);
        return true;
       default:
       return super.onContextItemSelected(item);
       }

public boolean onContextItemSelected // return type is of boolean

Edit:

  switch (menuid){
  case 0:
    Intent intnt2 = new Intent(ListActivity.this,aboutActivity.class);
    startActivity(intnt2);
    return true;
    ....
   default:
   return super.onContextItemSelected(item);
   }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

This is somewhat of a wild guess, but I'd check the menu item IDs. I'd be really surprised if they are 0 and 1; and in general, you shouldn't hardcode numbers, but use the R class constants.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169