私はGoogleの例を使用して、dialogfragmenthttp://developer.android.com/guide/topics/ui/controls/pickers.htmlを使用してアプリ内に日付ピッカーを挿入してい
ます。
しかし、設定後に日付を取得する方法がわかりません(Javaエキスパートではありません)。ダイアログとdatepickerは正常に実行され、日付が正しく設定されていることをログに記録できますが、親アクティビティでコールバックを実行するにはどうすればよいですか?
これは私のダイアログフラグメントです
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
**Log.w("DatePicker","Date = " + year);**
}
}
...そして私は自分のアクティビティからダイアログを呼び出します...
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
Log.wの代わりに親アクティビティでメソッドを呼び出す正しい方法は何ですか?これは、コールバックをパラメーターなどとして渡すことに関連していると思いますが、インターネットで見つけた参照のほとんどは、ダイアログフラグメントのない以前のバージョンに関するものです。
編集:それが重要かどうかはわかりませんが、親の活動は次のように宣言されています:
public class EditSessionActivity extends FragmentActivity {
解決策:Lechoユーザーのおかげで、これがその方法です
DatePickerFragmennt.class
public class DatePickerFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
}
}
...および親アクティビティEditSessionActivity.class....
public class EditSessionActivity extends FragmentActivity implements OnDateSetListener {
...
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((EditText) findViewById(R.id.tf_date)).setText("Date = " + year);
}