I have 4 checkboxes in my Alert Dialog: A, B, C and D. I am trying to get the behavior of making it so that A can only be selected with B, C or D, so there's only 2 selected at one time.
Would there be a solution to be able to achieve this behavior? I have looked at what I could find through Google, but I couldn't find anything. I think I am going to need to use share preferences, then once each of B, C and D are clicked, I could store a String in SP referencing to each of B, C , D, then it could check for the string in SP everytime one is clicked, and if one of the values stored from B,C or D are in there, uncheck that tick box from the one that has been ticked(hope this makes sense), or could there be a better solution? thanks
What I have so far:
boolean[] checked = new boolean[] { true, false, false, false }; //This is defined at the top of my class, so that once the dialog is first opnened, A is selected by default
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.filteroptions, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filter:
AlertDialog dialog;
final CharSequence[] items = { "A", "B", "C", "D" };
// arraylist to keep the selected items
final ArrayList<Integer> seletedItems = new ArrayList<Integer>();
shfObject = getActivity().getSharedPreferences("NAME",
Context.MODE_PRIVATE);
final SharedPreferences.Editor shfEditorObject = shfObject.edit();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Filter");
builder.setMultiChoiceItems(items, checked,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int indexSelected, boolean isChecked) {
if (isChecked) {
seletedItems.add(indexSelected);
switch (indexSelected) {
case 0:
shfEditorObject.putString(
"checkbox1Ticked", "Ticked1");
shfEditorObject.commit();
break;
case 1:
shfEditorObject.putString(
"checkbox2Ticked", "Ticked2");
shfEditorObject.commit();
break;
case 2:
shfEditorObject.putString(
"checkbox3Ticked", "Ticked3");
shfEditorObject.commit();
break;
case 3:
shfEditorObject.putString(
"checkbox4Ticked", "Ticked4");
shfEditorObject.commit();
break;
}
}
else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the
// array, remove it
// write your code when user Uchecked the
// checkbox
}
}
})
// Set the action buttons
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
// Your code when user clicked on OK
// You can write the code to save the
// selected item here
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
// Your code when user clicked on Cancel
}
});
dialog = builder.create();// AlertDialog dialog; create like this
// outside onClick
dialog.show();
return true;
default:
break;
}
return true;
}