private void initialization() { Date value = getValue(); dayToSelectList.clear(); hourToSelectList.clear(); SingleSelectionSpinner<DayToSelect> daySelector = (SingleSelectionSpinner<DayToSelect>) findViewById(R.id.datePicker1); SingleSelectionSpinner<HourToSelect> hourSelector = (SingleSelectionSpinner<HourToSelect>) findViewById(R.id.timePicker1); // build date LocalDateTime now = startDate; now = now.withMillisOfDay(0); now = now.withHourOfDay(0); for (int i = 0; i < maxDay; i++) { dayToSelectList.add(new DayToSelect(now, now.getDayOfMonth() + "/" + now.getMonthOfYear())); now = now.plusDays(1); } daySelector.setItems(dayToSelectList); // build date for (int i = 0; i <= 24; i++) { hourToSelectList.add(new HourToSelect(i, i + ":00")); } hourSelector.setItems(hourToSelectList); if (value != null) { setValue(value); } }
public void addChangeListener(final ChangeValue changeValue) { SingleSelectionSpinner<DayToSelect> daySelector = (SingleSelectionSpinner<DayToSelect>) findViewById(R.id.datePicker1); daySelector.setListener( new SingleSelectionSpinner.SingleSelectionSpinnerChangeListener<DayToSelect>() { @Override public void change(DayToSelect dayToSelect) { changeValue.changeValue(getValue()); } }); SingleSelectionSpinner<HourToSelect> hourSelector = (SingleSelectionSpinner<HourToSelect>) findViewById(R.id.timePicker1); hourSelector.setListener( new SingleSelectionSpinner.SingleSelectionSpinnerChangeListener<HourToSelect>() { @Override public void change(HourToSelect hourToSelect) { changeValue.changeValue(getValue()); } }); }
@Override public void setValue(Object value) { LocalDateTime localDateTime; if (value instanceof Date) { Date date = (Date) value; localDateTime = LocalDateTime.fromDateFields(date); } else if (value instanceof LocalDateTime) { localDateTime = (LocalDateTime) value; } else { return; } // hour SingleSelectionSpinner<HourToSelect> hourSelector = (SingleSelectionSpinner<HourToSelect>) findViewById(R.id.timePicker1); for (int i = 0; i < this.hourToSelectList.size(); i++) { HourToSelect hourToSelect = this.hourToSelectList.get(i); if (hourToSelect.getHour() == localDateTime.getHourOfDay()) { hourSelector.setSelection(i); break; } } // day localDateTime = localDateTime.withMillisOfDay(0); SingleSelectionSpinner<DayToSelect> daySelector = (SingleSelectionSpinner<DayToSelect>) findViewById(R.id.datePicker1); for (int i = 0; i < this.dayToSelectList.size(); i++) { DayToSelect dayToSelect = this.dayToSelectList.get(i); if (dayToSelect.getDate().equals(localDateTime)) { daySelector.setSelection(i); break; } } }
@Override public Object getValue(Class<?> type) { SingleSelectionSpinner<DayToSelect> daySelector = (SingleSelectionSpinner<DayToSelect>) findViewById(R.id.datePicker1); SingleSelectionSpinner<HourToSelect> hourSelector = (SingleSelectionSpinner<HourToSelect>) findViewById(R.id.timePicker1); if (daySelector.getSelectedItem() != null && hourSelector.getSelectedItem() != null) { LocalDateTime date = daySelector.getSelectedItem().getDate(); date = date.withHourOfDay(hourSelector.getSelectedItem().getHour()); return date.toDate(); } return null; }