0

Today imma trying to develop a simple app, which will ask the user to insert 2 dates by using the DatePickerDialog through a button, and then execute a query into a Database:

        Button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {


          Da = new DatePickerDialog(MainActivity.this, DADateSetListener, 2014, 8, 24);
          Da.show();
          Da.setTitle("Date FROM");

          A = new DatePickerDialog(MainActivity.this, ADateSetListener, 2014, 10, 24);

          A.show();
          A.setTitle("Date TO");


          //Erase the List View
          List.setAdapter(null);

          prepareProgressBar();
          query();

        }

    });

My only question is: How can i wait for the DatePickerDialog do dismiss or wait for the user to enter the input and then execute the query ?

Vesco
  • 138
  • 2
  • 15

2 Answers2

0

Per the docs:

We recommend that you use DialogFragment to host each time or date picker.

The docs include a sample class that extends DialogFragment (located here):

public static class TimePickerFragment extends DialogFragment
                            implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

And you would show it like this (again, code from the docs):

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}

And, as an added bonus, this dialog would indeed be modal (what you were going for when you said "wait for dismiss").

APerson
  • 8,140
  • 8
  • 35
  • 49
0

It's a simple dialog I don't see why you don't use the default listeners:

DatePickerDialog dpd;
dpd.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        // Handle dismiss
    }
});
dpd.setOnKeyListener(new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        // Handle clicks
        return false;
    }
});
Simas
  • 43,548
  • 10
  • 88
  • 116