2012-03-17 98 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