'Why is my startAlarm and updateTimeText not working?
I am trying to implement a time picker dialog using fragments and I want it so that the time chosen from the dialog is shown after choosing. However, after placing logcats, I found out that the startAlarm and updateTimeText functions weren't even running to begin with. Any ideas why? Here is my code for the onTimeSet that calls these 2 functions:
@Override
public void onTimeSet(android.widget.TimePicker timePicker, int hour, int minute) {
Log.e("time", "test1");
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
updateTimeText(c);
startAlarm(c);
Log.e("Time", "settime");
}
And here are the 2 functions in question:
private void updateTimeText(Calendar c){
String timeText = "Alarm set for: ";
timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());
// String timeText = "testing";
mTextView.setText(timeText);
Log.e("time", "time");
}
private void startAlarm(Calendar c){
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getContext(), AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 1, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
mTextView.setText("Testing");
Log.e("Start", "start");
}
Here is the TimePickerFragment dialog
public class TimePickerFragmentPage extends DialogFragment {
TimePickerFragmentPage()
{
}
TimePickerFragmentPage(TimePickerDialog.OnTimeSetListener onTimeSetListener){
this.onTimeSetListener = onTimeSetListener;
}
TimePickerDialog.OnTimeSetListener onTimeSetListener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
Log.e("Time", "Test2");
}
}, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
And here is the piece of code that references the dialog
if (id == R.id.action_set_time){
// move to time dialog
TimePicker dialog = new TimePicker();
// using bundle to pass room name and light inside dialog fragment
Bundle bundle = new Bundle();
bundle.putString("RoomID",light.getRoom());
bundle.putString("LightID",light.getId());
dialog.setArguments(bundle);
dialog.show(getParentFragmentManager(),"TimePicker");
}
Thanks in advance!
Solution 1:[1]
First, in your TimePickerFragmentPage, you never call the onTimeSetListener that you predefined when creating the dialog object.
I guess its redunant and you don't need it, or you want to use it instead of new TimePickerDialog.OnTimeSetListener (so instead of return new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener(){, I think you mean return new TimePickerDialog(getActivity(), onTimeSetListener());, and then when you create a TimePickerFragmentPage object you'd need to provide a listener.
Second, isn't the TimePickerFragmentPage object your dialog?
If it is, when you reference the dialog, you should call TimePickerFragmentPage dialog = new TimePickerFragmentDialog();, and not TimePicker dialog = new TimePicker(), because then you never even use the custom dialog you've created.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nitzan Daloomy |
