ここで私は私がTimePickerや日付ピッカーのいずれかを呼び出し、mDatemDateオブジェクトをTimePickerまたはDatePickerからCrimeFragmentに送り返すにはどうすればよいですか?
//OpenDialog
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mDate = (Date) getArguments().getSerializable(EXTRA_OPEN);
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
adb.setTitle(R.string.open_dialog_title);
adb.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FragmentManager fm = getActivity().getSupportFragmentManager();
switch (which) {
case DATE_DIALOG_INDEX:
dismiss();
// Somehow setTargetFragment
DatePickerFragment dialogDate = DatePickerFragment.newInstance(mDate);
dialogDate.show(fm,DIALOG_DATE);
break;
case TIME_DIALOG_INDEX:
dismiss();
TimePickerFragment dialogTime = TimePickerFragment.newInstance(mDate);
dialogTime.show(fm,DIALOG_TIME);
break;
default:
dismiss();
break;
}
}
});
return adb.create();
}
に価値を与え、ここに私のだよdate引数は、openDialogで
// CrimeFragment
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID crimeId = (UUID) getArguments().getSerializable(EXTRA_CRIME_ID);
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!= Activity.RESULT_OK) return;
Date date = new Date();
switch (requestCode) {
case REQUEST_DATE:
date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
break;
case REQUEST_TIME:
date = (Date) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
break;
}
mCrime.setmDate(date);
updateDate();
}
private void updateDate() {
mDateButton.setText(mCrime.getmDate().toString());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, container, false);
// SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, MMM dd, yyyy");
// String date = dateFormat.format(mCrime.getmDate());
mDateButton = (Button) v.findViewById(R.id.crime_date);
updateDate();
mDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
OpenDialog openDialog = OpenDialog.newInstance(mCrime.getmDate());
openDialog.show(fm,DIALOG_OPEN);
/*
//Working code
FragmentManager fm = getActivity().getSupportFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getmDate());
dialog.setTargetFragment(CrimeFragment.this,REQUEST_DATE);
dialog.show(fm,DIALOG_DATE);*/
}
});
では、openDialogを呼んでいます他の2つのクラス。
public Dialog onCreateDialog(Bundle savedInstanceState) {
View vDate = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
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);
DatePicker datePicker = (DatePicker) vDate.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) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
mDate = calendar.getTime();
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
return new AlertDialog.Builder(getActivity())
.setView(vDate)
.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);
Log.d("MyLog","DATE --> Gotta update real(mDate's date) " + mDate);
}
})
.create();
}
// TimePicker
public Dialog onCreateDialog(Bundle savedInstanceState) {
View vTime = getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);
mDate = (Date) getArguments().getSerializable(EXTRA_TIME);
// somehow need to set timePicker to mDate(hour, minute ? Or not needed
TimePicker timePicker = (TimePicker) vTime.findViewById(R.id.time_picker);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(mDate);
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
mDate = calendar.getTime();
getArguments().putSerializable(EXTRA_TIME,mDate);
}
});
return new AlertDialog.Builder(getActivity())
.setView(vTime)
.setTitle(R.string.time_picker_title)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
sendResult(Activity.RESULT_OK);
Log.d("MyLog", "TIME --> Gotta update real(mDate's time) " + mDate);
}
})
.create();
}
と私の質問は、私はどちらかから戻っCrimeFragmentにTimePickerDialogまたはDatePickerDialogからmDateを送ることができる方法です??それらからsetTargetFragmentをCrimeFragmentに設定し、それをonActivityResult()で処理することは可能ですか?どうやってやるの ?