I have 4 checkboxes in my Alert dialog box: A, B, C and D. I'm trying to get the behavior to make it so that A can only be selected with B, C or D, so there were only 2 selected time.
Will there be a solution to achieve this behavior? I looked at what I can find through Google, but found nothing. I think I will need to use the share settings, and then, by clicking on each of B, C and D, I can save the String in SP, referring to each of B, C, D, then it can check the string in SP every time, when you click, and if one of the values stored in B, C or D is there, uncheck the box that was checked (hope this makes sense), or maybe the best solution? thank
What I still have:
boolean [] checked = new boolean [] {true, false, false, false}; // This is defined at the top of my class, so that as soon as the dialog is first, 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" };
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)) {
}
}
})
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
}
});
dialog = builder.create();
dialog.show();
return true;
default:
break;
}
return true;
}