Android: Timepickerdialog - Timepickerdialog.ontimesetlistener
Solution 1:
In AddPeriod.java you create a TimePickerDialog.OnTimeSetListener but as far as I can see that listener isn't used.
In TimePickerFragment.java you implement the TimePickerDialog.OnTimeSetListener interface and pass the fragment instance itself as a listener to the TimePickerDialog but you do nothing in the implemented onTimeSet() method in TimePickerFragment.java. Are you saying that onTimeSet() in TimePickerFragment.java isn't called? Have you set a breakpoint?
If you manage to get the onTimeSet() called in your fragment I assume what you're really asking for is how to pass this time back to the activity. If I'm correct I'd say that this is a perfect place to use an event bus, for instance using Otto.
UPDATE: Based on your comments to my questions I assume that what you really want is a way to get the picked time in TimePickerFragment back to AddPeriod. The better approach is in my opinion to use an event bus (as mentioned above). This will decouple your fragment from your activity. BUT to give you an answer that works with minimal changes to your code I suggest that you pass a listener to the fragment and use that listener when you create your dialog:
TimePickerFragment.java:
publicclassTimePickerFragmentextendsDialogFragment {
private TimePickerDialog.OnTimeSetListener listener;
publicTimePickerFragment(TimePickerDialog.OnTimeSetListener listener) {
super();
this.listener = listener;
}
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the pickerfinalCalendarc= Calendar.getInstance();
inthour= c.get(Calendar.HOUR_OF_DAY);
intminute= c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return itreturnnewTimePickerDialog(getActivity(), listener, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
}
As you can see I've added a constructor that takes a TimePickerDialog.OnTimeSetListener argument. This listener is used when the TimePickerDialog is created. Now in our AddPeriod activity we pass the mTimeSetListener to the fragment when it is created:
publicclassAddPeriodextendsSherlockFragmentActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_period);
}
publicvoidshowTimePickerDialog(View v) {
DialogFragmentnewFragment=newTimePickerFragment(mTimeSetListener);
newFragment.show(getSupportFragmentManager(), "timePicker");
}
TimePickerDialog.OnTimeSetListenermTimeSetListener=newTimePickerDialog.OnTimeSetListener() {
@OverridepublicvoidonTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
Log.i("",""+hourOfDay+":"+minute);
}
};
}
Post a Comment for "Android: Timepickerdialog - Timepickerdialog.ontimesetlistener"