Try this and improve the structure of your code to manage it effectively.
import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private Button btn; private TextView txtSelected; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn_dialog); txtSelected = (TextView) findViewById(R.id.txt_selected); final ArrayList<ColorVO> colorList = new ArrayList<ColorVO>(); // String array for alert dialog multi choice items final 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 }; btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); // make a list to hold state of every color for (int i = 0; i < colors.length; i++) { ColorVO colorVO = new ColorVO(); colorVO.setName(colors[i]); colorVO.setSelected(checkedColors[i]); colorList.add(colorVO); } // Do something here to pass only arraylist on this both arrays ('colors' & 'checkedColors') builder.setMultiChoiceItems(colors, checkedColors, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // set state to vo in list colorList.get(which).setSelected(isChecked); Toast.makeText(getApplicationContext(), colorList.get(which).getName() + " " + isChecked, Toast.LENGTH_SHORT).show(); } }); builder.setCancelable(false); builder.setTitle("Your preferred colors?"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { txtSelected.setText("Your preferred colors..... \n"); // save state of selected vos ArrayList<ColorVO> selectedList = new ArrayList<>(); for (int i = 0; i < colorList.size(); i++) { ColorVO colorVO = colorList.get(i); colors[i] = colorVO.getName(); checkedColors[i] = colorVO.isSelected(); if (colorVO.isSelected()) { selectedList.add(colorVO); } } for (int i = 0; i < selectedList.size(); i++) { // if element is last then not attach comma or attach it if (i != selectedList.size() - 1) txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName() + " ,"); else txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName()); } colorList.clear(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // make sure to clear list that duplication dont formed here colorList.clear(); } }); builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // make sure to clear list that duplication dont formed here colorList.clear(); } }); AlertDialog dialog = builder.create(); dialog.show(); } }); } private class ColorVO { private String name; private boolean selected; public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } }
Solved all your inquiries:
1. How I chose Red and Purple
=> Check the index of the element, if it is the last, then do not attach a comma.
2. I already selected Red and Purple when I open the dialog again without getting red and purple by default (how can I save the state)
=> save state with vo in your default array. It will be saved until your activity is destroyed.
3. when I select these (red and purple) two elements again, getting each element twice in the TextView
=> Clear the list so that the values ββare not duplicated.
Andigeeky
source share