2012-03-17 97 views
0

我的问题是如何从上下文菜单切换到正确的活动。从上下文菜单切换到正确的活动

我有这样的活动:

  • Main
  • AccelerometerOptionsActivity
  • GyroscopeOptionsActivity
  • OrientationOptionsActivity

在主要活动中我有传感器的列表。当我点击传感器时,会出现上下文菜单,我可以点击例如选项。

我的问题是如何从上下文菜单切换到所选传感器的选项活动。 我的代码:

@Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
     int menuItemIndex = item.getItemId(); 
     String[] menuItems = getResources().getStringArray(R.array.sensor_array); 
     String menuItemName = menuItems[menuItemIndex]; 


     if(item.getTitle()=="Start Service"){ 
      Toast.makeText(this,"Start " + menuItemName+ " selected", Toast.LENGTH_SHORT).show(); 
     } else if(item.getTitle()=="Stop Service") { 
      Toast.makeText(this,"Stop " + menuItemName+ " selected", Toast.LENGTH_SHORT).show(); 
     } else if(item.getTitle()=="Options") { 


       Intent options = new Intent(this, AccelerometerOptionsActivity.class); 
       startActivity(options); 

     } 

     return true; 
    } 

UPDATE:

下面是代码:

@Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
     if (v.getId()==R.id.list) { 
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; 

      menu.setHeaderTitle(sensorsArray[info.position]); 
      menu.add(Menu.NONE, CONTEXTMENU_START, 0, "Start Service"); 
      menu.add(Menu.NONE, CONTEXTMENU_STOP, 1, "Stop Service"); 
      menu.add(Menu.NONE, CONTEXTMENU_OPTIONS, 2, "Options"); 
      menu.add(Menu.NONE, CONTEXTMENU_GRAPHS, 3, "Graph view"); 
      menu.add(Menu.NONE, CONTEXTMENU_DATA, 4, "Data view"); 
     } 
    } 
+0

你有一个'ListView'与传感器上(名单)您设置的'ContextMenu' ? – Luksprog 2012-03-17 20:29:21

+0

将代码发布到显示上下文菜单的位置。 – 2012-03-17 21:18:14

+0

是的,当我点击ContextMenu出现的传感器名称时,我有一个带有传感器的ListView。我在上面的问题中添加了代码。 – erni 2012-03-18 11:15:45

回答

0

onContextItemSelected()试试这个:

@Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item 
        .getMenuInfo(); 
     int menuItemIndex = item.getItemId(); // the position of the item in the list with the context menu 
     String[] menuItems = getResources().getStringArray(R.array.sensor_array); 
     String menuItemName = menuItems[menuItemIndex]; 
     switch (item.getItemId()) { 
     case CONTEXTMENU_START: 
      Toast.makeText(this, "Start " + menuItemName + " selected", 
         Toast.LENGTH_SHORT).show(); 
     break; 
     case CONTEXTMENU_STOP: 
      Toast.makeText(this, "Stop " + menuItemName + " selected", 
        Toast.LENGTH_SHORT).show(); 
     break; 
      case CONTEXTMENU_OPTIONS: 
     // here start the correct options activity either by checking for 
     // String equality with menuItemName or by doing a switch on the menuItemIndex(this works 
     // if your list of sensors is based on the array R.array.sensor_array) 
      switch (menuItemIndex) { 
       case 0: 
       //the user clicked the first sensor in the list so start that option activity   
       break; 
       case 1: 
       //the user clicked the second sensor in the list so start that option activity 
        break; 
       } 
     } 
     return super.onContextItemSelected(item); 
} 
+0

我试过你的解决方案,但它不工作。我调试了代码,它省略了switch语句。 – erni 2012-03-18 19:41:38

+0

@erni在我上面的代码中'int menuItemIndex = item.getItemId();'用'int menuItemIndex = info.position;' – Luksprog 2012-03-18 19:53:56

+0

感谢它的工作! – erni 2012-03-18 20:23:22

0

使用。载有(yourString)方法,而不是==。 例如更换

if(item.getTitle()=="Options") {... 

if(item.getTitle().contains("Options") {... 
+0

我改变了这个开关。但这不是我的问题。我的问题是要得到适当的活动。 – erni 2012-03-17 20:26:28

+0

你的代码看起来很好,除了那个==部分。替换它并看看。尝试调试是否可行,看看它是否触及断点。如果您收到任何异常或其他问题,请将logcat中的日志与您的问题进行比较。 – Rajkiran 2012-03-17 20:33:27

+0

我认为你不了解我的问题。我希望被引导到正确的选项活动,例如我选择加速度计,并从上下文菜单中选择我想要加速计选项。我选择定位服务,从上下文菜单中选择定位选项等。==这部分工作正常。问候 – erni 2012-03-18 11:44:22