これは私の最初の投稿で、非常に単純なタスカー アプリを作成しようとしていますが、onDateChanged メソッド (と思われるもの) に問題があります。ダイアログが正常にポップアップしても、変更を実装していないようです。ログにエラーはなく、同様の機能を持つさまざまなソリューションを再確認しましたが、関連するものが見つかりませんでした。(minSDK 16、ターゲット 21)
事前に感謝します。それが明らかな場合は申し訳ありません。
public class DatePickerFragment extends DialogFragment {
public static final String EXTRA_DATE = "thanoschatz.com.annastodolist.date";
private Date mDate;
public static DatePickerFragment newInstance(Date mDate) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_DATE, mDate);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(mDate);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_datepicker);
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDate = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTime();
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
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 dialog, int which) {
sendResult(Activity.RESULT_OK);
}
})
.create();
}
private void sendResult(int resultCode) {
if (getTargetFragment() == null) return;
Intent intent = new Intent();
intent.putExtra(EXTRA_DATE, mDate);
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
}
}
public class ToDoFragment extends Fragment {
public static final String EXTRA_TODO_ID = "thanoschatz.com.annastodolist.todo_id";
private static final String DIALOG_DATE = "date";
private static final int REQUEST_DATE = 0;
private ToDo mToDo;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mDoneCheckBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
UUID toDoId = (UUID)getArguments().getSerializable(EXTRA_TODO_ID);
mToDo= ToDoLab.get(getActivity()).getToDo(toDoId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo, container, false);
wireTitleField(view);
wireDateButton(view);
wireSolvedCheckBox(view);
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date mDate = (Date)intent.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mToDo.setDate(mDate);
}
}
public ToDoFragment(){
}
public static ToDoFragment newInstance(UUID mId) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_TODO_ID, mId);
ToDoFragment fragment = new ToDoFragment();
fragment.setArguments(args);
return fragment;
}
private void wireDateButton(View view) {
SimpleDateFormat dateFormatter = getSimpleDateFormat();
mDateButton = (Button)view.findViewById(R.id.toDo_date);
updateDate(dateFormatter);
mDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
DatePickerFragment datePicker = DatePickerFragment.newInstance(mToDo.getDate());
datePicker.setTargetFragment(ToDoFragment.this, REQUEST_DATE);
datePicker.show(fragmentManager, DIALOG_DATE);
}
});
}
}