从上下文菜单切换到正确的活动

时间:2012-03-17 18:57:54

标签: android android-intent

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

我有这样的活动:

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

更新:

以下是代码:

 @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");
         }  
    }

2 个答案:

答案 0 :(得分:0)

使用.contains(yourString)方法而不是==。 例如替换

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

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

答案 1 :(得分: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);
}