2015-04-27 67 views
0

我有一個數組,我顯示在一個AlertDialog中,我想在點擊該行開始一個活動並傳遞一些數據。 我創建了這個代碼,但是如果它是數組有三個元素,則打開三個活動。我錯在哪裏?謝謝Android項目的開始活動

final String[] choiceList = nome_op; 
final String[] idOperatoriList = id_nome_op; 
    builder2.setItems(choiceList, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      for (int i = 0; i < choiceList.length; i++) { 
       Intent singole_usc = new Intent(getActivity(), Inserisci_m.class); 
       Bundle extras = new Bundle(); 
       extras.putString("id", idOperatoriList[item]); 
       extras.putLong("data", calendarioFooter.getTimeInMillis()); 
       singole_usc.putExtras(extras); 
       startActivity(singole_usc); 
      } 
     } 
    }) 
... 
+0

您置於startActivity方法中的條件無環路。它顯然會打開與choiceList數組長度相同數量的活動 –

回答

1

看起來你只需要把你的代碼從for循環中取出。像這樣,

final String[] choiceList = nome_op; 
final String[] idOperatoriList = id_nome_op; 
    builder2.setItems(choiceList, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int item) { 

      Intent singole_usc = new Intent(getActivity(), Inserisci_m.class); 
      Bundle extras = new Bundle(); 
      extras.putString("id", idOperatoriList[item]); 
      extras.putLong("data", calendarioFooter.getTimeInMillis()); 
      singole_usc.putExtras(extras); 
      startActivity(singole_usc); 

    } 
}) 

索引變量i永遠不會在塊中使用。

0

具有startActivity()的外部for循環

Intent singole_usc = new Intent(getActivity(), 
for (int i = 0; 
i < choiceList.length; i++) { 
    Inserisci_m.class); 
    Bundle extras = new Bundle(); 
    extras.putString("id", idOperatoriList[item]); 
    extras.putLong("data", calendarioFooter.getTimeInMillis()); 
    singole_usc.putExtras(extras); 
} 

startActivity(singole_usc);