I need to use DatePicker dialog using the new DialogFragment class for multiple activities. I am having more than two activities using datepicker and time picker. I succeeded using the example in developer.android.com for Dialog Fragment for single activity.
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
String date;
Bundle bundle;
Intent in;
//private View v;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(),(Pickup)getActivity(), year, month, day);
}
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
}
If I use that I have to write different DialogFragment classes for each activities. Is there any solution like using switch case provided in this example?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnewreminder);
initialize();
context = getApplicationContext();
OnClickListener listenerDate = new OnClickListener() {
@Override
public void onClick(View arg0) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
}
};
editTextDate.setOnClickListener(listenerDate);
}
private void initialize() {
// TODO Auto-generated method stub
editTextDate = (EditText) findViewById(R.id.editTextDate);
}
private void updateDisplay() {
currentDate = new StringBuilder().append(day).append(".")
.append(month + 1).append(".").append(year).toString();
Log.i("DATE", currentDate);
}
OnDateSetListener myDateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int j, int k) {
year = i;
month = j;
day = k;
updateDisplay();
editTextDate.setText(currentDate);
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, myDateSetListener, year, month,
day);
}
return null;
}