Comment puis-je Contrôler sur MultiChoice AlertDialog

Je suis en utilisant la boîte de Dialogue dans mon application pour permettre à l'utilisateur de faire une sélection multiple, Voici mon code:

    btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Build an AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//String array for alert dialog multi choice items
String[] colors = new String[]{
"Red",
"Green",
"Blue",
"Purple",
"Olive"
};
//Boolean array for initial selected items
final boolean[] checkedColors = new boolean[]{
false, //Red
false, //Green
false, //Blue
false, //Purple
false //Olive
};
//Convert the color array to list
final List<String> colorsList = Arrays.asList(colors);
//Set multiple choice items for alert dialog
builder.setMultiChoiceItems(colors, checkedColors, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//Update the current focused item's checked status
checkedColors[which] = isChecked;
//Get the current focused item
String currentItem = colorsList.get(which);
//Notify the current action
Toast.makeText(getApplicationContext(),
currentItem + " " + isChecked, Toast.LENGTH_SHORT).show();
}
});
//Specify the dialog is not cancelable
builder.setCancelable(false);
//Set a title for alert dialog
builder.setTitle("Your preferred colors?");
//Set the positive/yes button click listener
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do something when click positive button
tv.setText("Your preferred colors..... \n");
for (int i = 0; i<checkedColors.length; i++){
boolean checked = checkedColors[i];
if (checked) {
tv.setText(tv.getText() + colorsList.get(i) + ", ");
}
}
}
});
//Set the negative/no button click listener
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do something when click the negative button
}
});
//Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do something when click the neutral button
}
});
AlertDialog dialog = builder.create();
//Display the alert dialog on interface
dialog.show();
}
});

Et j'ai deux requêtes:

  1. Comme j'ai choisi le Rouge et le Violet

    (TextView l'obtention de la sortie comme ceci: Red, Purple,)

    Tout d'abord, je voudrais enlever la virgule (qui devient avec la dernière valeur)

  2. J'ai déjà choisi le Rouge et le Violet, lorsque j'ai à nouveau ouvrir la boîte de dialogue de ne pas obtenir le rouge et le violet, tel que sélectionné par défaut (Comment puis-je économiser de l'état)enter code here, et comme un résultat, quand je suis de nouveau la sélection de ces (Rouge et Violet) deux éléments, l'obtention de chaque article deux fois dans un TextView

OriginalL'auteur Oreo | 2015-09-01