0

I have my Main Activity with a button, which shows a Dialog with 3 Options from a string-array. The Dialog / Popup work, but how can i get the choice of the user in my main activity?

Function of main activity (yeah, need an int or so as return):

    public void showDialog(View view)
{
    DifDialog dia = new DifDialog();
    dia.show(getFragmentManager(), "test");
}

class DifDialog:

public class DifDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.difficulty);
    builder.setItems(R.array.diff, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    Dialog dialog=builder.create();


return dialog; }}

I need the "which" from the onClick in my main activity.. (i think ;)

Noxmiles
  • 45
  • 1
  • 7
  • What about something like this? http://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment – Rick S Jul 09 '15 at 17:53

2 Answers2

1

If your goal is to have the dialog tell the activity which button was clicked, then you can use an interface as a callback from the dialog to the activity.

To do this you should first define the interface in your dialog fragment.

In your activity when you show a dialog fragment there is an acivity lifecycle method called onAttachFragment() that gets called. When this gets called you check to see that the fragment being added is your dialog fragment. If it is, this is where you set the listener.

Now when your button is clicked in your dialog fragment you can make a callback to the activity using the listener.

In your dialog fragment code:

public class DifDialog extends DialogFragment {

private DifDialogListener mListener;

public interface DifDialogListener {
    void onButtonOneClicked();
    void onButtonTwoClicked();
    void onButtonThreeClicked();
}

public void setListener(DifDialogListener listener) {
    if (listener == null) {
       throw new NullPointerException();
    }
    mListener = listener;
}

    public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.difficulty);
    builder.setItems(R.array.diff, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       if (which == DialogInterface.BUTTON_POSITIVE) {
           mListener.onButtonOneClicked();
       } else if (which == DialogInterface.BUTTON_NEGATIVE) {
           mListener.onButtonTwoClicked();
       } else if (which == DialogInterface.BUTTON_NEUTRAL) {
           mListener.onButtonThreeClicked();
       }
});

Dialog dialog=builder.create();

return dialog; }}

In your activity:

showDialog() {
    DifDialog dia = new DifDialog();
    dia.show(getFragmentManager(), "test");
}

@Override
onAttach(Fragment fragment) {
    if (fragment instanceOf DifDialog) {
        DifDialog frag = (DifDialog) fragment;
        frag.setListener(new DifDialogListener() {
            @Override
            onButtonOneClicked() {
                //Button one was clicked
            }

            @Override
            onButtonTwoClicked() {
                 //Button two was clicked
            }

            @Override
            onButtonThreeClicked() {
                 //Button three was clicked
            }
        });

    }
}
neonDion
  • 2,278
  • 2
  • 20
  • 39
0

Hope this helps

    List<String> optionsList = new ArrayList<String>();

    optionsList.add("Option One");
    optionsList.add("Option Two");
    optionsList.add("Option Three");

    final CharSequence[] options = optionsList.toArray(new CharSequence[optionsList.size()]);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Title");

    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (options[item].equals("Option One")) {
               //do something
            } else if (options[item].equals("Option Two")) {
               //do something
            } else if (options[item].equals("Option Three")) {
               //do something
            } 
        }
    });
    builder.show();
}
Jaseem Abbas
  • 5,028
  • 6
  • 48
  • 73