我有一个我在AlertDialog中显示的数组,我想在点击该行时启动一个活动并传递一些数据。 我创建了这个代码,但如果是数组有三个元素,则打开三个活动。我哪里错了?感谢
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 :(得分: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从不在块中使用。
答案 1 :(得分:0)
在for循环外面有startActivity()
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);