Reputation: 3
I´ve a Dialog where I can select several things, in my example ingredients for a pizza. I want to choose/select more than one item. But everytime I run the app, I can select more than one but it displays only one. Here´s my code, I overlooked it more then once but couldn´t find my mistake. I hope you can help me. Thanks in advance ;)
private void pizzaBelagDialog() {
// Variablen
final ArrayList<Integer> arrayBelag = new ArrayList<Integer>();
dialogBuilder = new AlertDialog.Builder(this);
final String[] strBelaege = { "Hühnchen", "Pepperoni", "Pilze",
"Zwiebeln", "Speck", "Oliven", "Ananas", "Pommes", "Soße",
"Meeresfrüchte" };
// Process
strBelag = "\nBelag:\n";
dialogBuilder.setTitle("Wähle deinen Belag aus");
dialogBuilder.setMultiChoiceItems(strBelaege, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
arrayBelag.add(which);
} else if (arrayBelag.contains(which)) {
arrayBelag.remove(Integer.valueOf(which));
}
}
});
dialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int intLoop = 0; intLoop < arrayBelag.size(); intLoop++) {
strBelag = strBelaege[(Integer) arrayBelag
.get(intLoop)] + ", ";
}
Toast.makeText(getApplicationContext(),
"Belag wurde ausgewählt.", Toast.LENGTH_SHORT)
.show();
Display();
}
});
dialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"Belag wurde nicht ausgewählt.",
Toast.LENGTH_SHORT).show();
}
});
// Output
AlertDialog dialogPizzaBelag = dialogBuilder.create();
dialogPizzaBelag.show();
}
Syntac
Upvotes: 0
Views: 43
Reputation: 271
In every loop you override your variable strBelag. You should use this:
strBelag += your code + ", ".
This way you keep the value you added in the previous loop and don't change it.
Upvotes: 1