Here is my structure: I am running an AlertDialog from a fragment that extends DialogFragment. It is a custom AlertDialog that is inflated from XML, and is using .setMultiChoiceItems. There will be many different checkboxes used on different AlertDialogs and on different fragments. My main activity implements a DialogInterface.OnDismissListener, which will do things after they've checked all the checkboxes and click "OK".
Here is what I want to do: Directly check the state of all the CheckBoxes in the AlertDialog without loading them into an array, and set public variables in the MainActivity based on the results. I can perform additional logic and actions at that time, based on the variables.
All the examples and tutorials I have found create an array, and access the entries via the selection integer. I would love to keep it simple and do something like:
public class FiltersFragment extends DialogFragment {
@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
final Activity activity = getActivity();
if (activity instanceof DialogInterface.OnDismissListener) {
((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
if (MainActivity.clickedID == 1) {
View view = getActivity().getLayoutInflater().inflate(
R.layout.dialog_filters, null);
builder.setView(view)
.setIcon(R.drawable.filtersicon)
.setTitle(R.string.sortbyfilters)
.setMultiChoiceItems(null, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int index, boolean isChecked) {
CheckBox option1 = (CheckBox) getActivity().findViewById(R.id.option1);
CheckBox option2 = (CheckBox) getActivity().findViewById(R.id.option2);
CheckBox option3 = (CheckBox) getActivity().findViewById(R.id.option3);
if (option1.isChecked()) {
MainActivity.option1result = true;
}
if (option2.isChecked()) {
MainActivity.option2result = true;
}
if (option3.isChecked()) {
MainActivity.option3result = true;
}
}
});
Button button= (Button) view.findViewById(R.id.ok_button);
final AlertDialog dialog = builder.create();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
return dialog;
}
That way, any time I want, I can simply say: if (MainActivity.option3) {do this} else {do that}... instead of searching an array for whatever text option 3 has, etc.
No matter what I do, those MainActivity variables are not setting to true, because it doesn't seem to be assessing the CheckBoxes properly.
Also, I am not an expert and have never taken classes on java programming and am a visual learner, so if at all possible, please use code examples. Telling me to just instantiate the method with proper callbacks to the object's blah blah, or directing me to read the android website is really not helpful :) Thanks in advance for your time.
Aucun commentaire:
Enregistrer un commentaire