I'm trying to create an alert dialog where the user can select multiple options from a pre-defined list, but if the option(s) they want to select aren't there, they can add them (via an edittext and "add" button).
So far, I've added the edittext and button to the top of the dialog using setCustomTitle, and I'm able to add the item to my source data list, but I can't figure out how to reapply this to the list in the dialog.
(Filter model just has a string and a boolean to determine whether it's selected, so if the user reopens the dialog I can set the selected items)
Here's my code so far...
private void selectSomething() {
AlertDialog.Builder selectSomethingBuilder = new AlertDialog.Builder(mContext);
View header = getLayoutInflater().inflate(R.layout.common_multichoice_dialog_header, null);
Button addButton = header.findViewById(R.id.btAdd);
EditText et = header.findViewById(R.id.textView2);
selectSomethingBuilder.setCustomTitle(header);
addButton.setOnClickListener(v -> {
cuisineList.add(new filterModel(et.getText().toString(), true));
boolean[] selectedItems2 = new boolean[cuisineList.size()];
for (int i = 0; i < cuisineList.size(); i++) {
selectedItems2[i] = cuisineList.get(i).isSelected();
}
String[] updatedList = cuisineList.stream().map(filterModel::toString).toArray(String[]::new);
//What to do here? How do I update the list of items with the newly added item
et.setText("");
});
boolean[] selectedItems = new boolean[cuisineList.size()];
for (int i = 0; i < cuisineList.size(); i++) {
selectedItems[i] = cuisineList.get(i).isSelected();
}
selectSomethingBuilder.setMultiChoiceItems(cuisineList.stream().map(filterModel::toString).toArray(String[]::new), selectedItems,
(dialog, which, isChecked) -> cuisineList.get(which).setSelected(isChecked));
selectSomethingBuilder.setPositiveButton(R.string.ok, (dialog, id1) -> {
setCuisineChips();
});
AlertDialog selectItems = selectSomethingBuilder.create();
selectItems.show();
}