0

ここで私は私が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()で処理することは可能ですか?どうやってやるの ?

答えて

0

リスナーパターンを使用します。 Dialogクラスに、ホストアクティビティ/フラグメントが実装するインタフェースを作成します。

ダイアログのonAttachで、ホストからリスナーを取得します。

Android documentationには、この通信の実行方法が示されています。ここではthierページの例です:

public class NoticeDialogFragment extends DialogFragment { 

/* The activity that creates an instance of this dialog fragment must 
* implement this interface in order to receive event callbacks. 
* Each method passes the DialogFragment in case the host needs to query it. */ 
public interface NoticeDialogListener { 
    public void onDialogPositiveClick(DialogFragment dialog); 
    public void onDialogNegativeClick(DialogFragment dialog); 
} 

// Use this instance of the interface to deliver action events 
NoticeDialogListener mListener; 

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener 
@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    // Verify that the host activity implements the callback interface 
    try { 
     // Instantiate the NoticeDialogListener so we can send events to the host 
     mListener = (NoticeDialogListener) activity; 
    } catch (ClassCastException e) { 
     // The activity doesn't implement the interface, throw exception 
     throw new ClassCastException(activity.toString() 
       + " must implement NoticeDialogListener"); 
    } 
} 
... 

}

関連する問題