I have a DatePickerFragment class and an associated button with it. As a result, I have a window with a choice of date. I want to make the same window, only with the choice of time TimePickerFragment. Because the classes are similar, they need to be changed a little, but something went wrong with me. At the end I highlighted the problem area with comments. What needs to be changed?

public class DatePickerFragment extends DialogFragment { public static final String EXTRA_DATE = "com.diplom.android.diplom.taskIntent.date"; private static final String ARG_DATE = "date"; private DatePicker mDatePicker; //Передача даты через аргументы = объект дэйт аргумент фрагмента public static DatePickerFragment newInstance(Date date) { Bundle args = new Bundle(); args.putSerializable(ARG_DATE, date); DatePickerFragment fragment = new DatePickerFragment(); fragment.setArguments(args); return fragment; } @Override public Dialog onCreateDialog(Bundle savedInstanceState){ Date date = (Date) getArguments().getSerializable(ARG_DATE); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int mouth = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); View v = LayoutInflater.from(getActivity()) .inflate(R.layout.dialog_date, null); mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker); mDatePicker.init(year, mouth, day, null); return new AlertDialog.Builder(getActivity()) .setView(v) .setTitle(R.string.date_picker_title) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { int year = mDatePicker.getYear(); int month = mDatePicker.getMonth(); int day = mDatePicker.getDayOfMonth(); Date date = new GregorianCalendar(year, month, day).getTime(); sendResult(Activity.RESULT_OK, date); } }) .create(); } private void sendResult(int resultCode, Date date){ if (getTargetFragment() == null) { return; } Intent intent = new Intent(); intent.putExtra(EXTRA_DATE, date); getTargetFragment() .onActivityResult(getTargetRequestCode(), resultCode, intent); } 

}

And the button with which it is associated

  mDateButton = (Button) v.findViewById(R.id.task_date_title); updateDate(); mDateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager manager = getFragmentManager(); DatePickerFragment dialog = DatePickerFragment.newInstance(mTask.getDate()); dialog.setTargetFragment(TaskFragment.this, Request_DATE); dialog.show(manager, DIALOG_DATE); } }); 

I need to make a TimePickerFragment. That's what I got.

 @Override public Dialog onCreateDialog(Bundle savedInstanceState){ Date date = (Date) getArguments().getSerializable(ARG_TIME); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); //calendar.setTime(date); // mTimePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); // mTimePicker.setCurrentMinute(calendar.get(Calendar.MINUTE)); int hours = calendar.get(Calendar.HOUR); int minutes = calendar.get(Calendar.MINUTE); View v = LayoutInflater.from(getActivity()) .inflate(R.layout.dialog_time, null); mTimePicker = (TimePicker) v.findViewById(R.id.dialog_time_picker); mTimePicker.setIs24HourView(true); mTimePicker.setCurrentHour(new Integer(minutes)); mTimePicker.setCurrentHour(new Integer(hours)); // mTimePicker.init(year, mouth, day, null); return new AlertDialog.Builder(getActivity()) .setView(v) .setTitle(R.string.time_picker_title) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ....//Вот здесь я не знаю, что сделать, чтобы ......//вернуть нужный мне объект со временем int hours = mTimePicker.getHour(); int minuts = mTimePicker.getMinute(); Date time = ; sendResult(Activity.RESULT_OK, time); } }) .create(); } private void sendResult(int resultCode, Date time){ if (getTargetFragment() == null) { return; } Intent intent = new Intent(); intent.putExtra(EXTRA_TIME, time); getTargetFragment() .onActivityResult(getTargetRequestCode(), resultCode, intent); } 

    2 answers 2

    We take minutes and hours, we translate in milliseconds.

    Code:

     //Перевели в миллисекунды: long minutes = TimeUnit.MINUTES.toMillis(minutes); long hours = TimeUnit.HOURS.toMillis(hours); // Создали дату Date newDate = Date(hours + minutes); 

    thanks @pavlofff (y)

    • 2
      There is already a callback in the code in the code, the result is returned via onActivityResult() . The problem in question is how to pack hours and minutes (not years, months and days) in the Date type - pavlofff
    • @pavlofff and what not to translate everything into seconds and throw in the Date? - Timur Mukhortov
    • one
      Well, you need to ask the author. - pavlofff 2:24 pm
    • Yes, that's right, how to pack Time into a Date. What kind of dismiss ()? - KeyJibo 2:24 pm
    • dismiss it after you have done everything you wanted to close your dialogue - Timur Mukhortov

    This is from hardy. Soryan for the spoiler, but I did this:

     return new AlertDialog.Builder(getActivity()) .setView(v) .setTitle(R.string.time_picker_title) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { clock.set(Calendar.HOUR,mTimePicker.getCurrentHour()); //это экземпляр Calendar clock.set(Calendar.MINUTE,mTimePicker.getCurrentMinute()); //это экземпляр Calendar date.setTime(clock.getTimeInMillis()); sendResult(Activity.RESULT_OK, date); } }) .create(); 

    here are outdated methods (getCurrentHour (), getCurrentMinute ()), but it is easy to fix by deleting the word Current in the method call, but the compiler does not allow me to do this, as I wrote under the API of 19, and these methods are supported by API not lower than 23 , and checking the conditions of the installed version of Andrew I was too lazy to write. All daisies and less tediousness

    • Can you explain how your 2 answers differ and why you shouldn’t write them alone? - Qwertiy
    • I found a solution, but it is a bit different. There are many bugs, I will check your version, I hope he will help me. - KeyJibo
    • Please put both code variants in one answer with explanations of how they differ. I think it will be so fenshuy) - YuriySPb
    • I checked the truth from the side. The problem is that the DatePicker and the TimePicker are glittering different values ​​into one variable. I did not think up how to complain about a date into a variable without time, and then go back to the same time without a date. The way out was to create a 5 variable in the object that is responsible for the year, month, etc. and pass the object these values. Then they just lift through get. The method is theoretical, and therefore, as I check, I will write it off. - KeyJibo