Date changed, time is gone and vice-versa. Please help!


#1

Hello Guys, Ok… The following is the resolution code that I’ve got from my limited knowledge.
First, let me say that I still have an issue though, so when I set the date, it gets changed correctly, but then when I set the minutes, the date is gone, so it doesn’t keep me the date specified previously… And when I start setting the time, it gets changed correctly, but the date is gone, in other words, it’s not persistent either.

Within the CrimeFragment.java class, the mDateButton is calling an instance of a new class called DateTimeFragment.java, which will be holding the dialog list for selecting either the date or time.

...
		mDateButton = (Button)v.findViewById(R.id.crime_date);
		//mDateButton.setText(mCrime.getDate().toString());
		// The following function replaces the above line.
		updateDate();
		//mDateButton.setEnabled(false);
		mDateButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FragmentManager fm = getActivity().getSupportFragmentManager();				
				DateTimeFragment dialogDateTime = new DateTimeFragment().newInstance(mCrime.getDate());								
				dialogDateTime.setTargetFragment(CrimeFragment.this, REQUEST_DATETIME);
				dialogDateTime.show(fm, DIALOG_DATETIME);
			}
		});
...

Then on the “which” value within the event listener “public void onClick(DialogInterface dialog, int which)”, I’m calling an instance of either date or time, depends on what the user selected.
DateTimeFragment.java

...
package com.bignerdranch.android.criminalintent;

import...

public class DateTimeFragment extends DialogFragment {
	
	...
	@Override 
	public Dialog onCreateDialog(Bundle savedInstanceState){
		mDate = (Date)getArguments().getSerializable(EXTRA_DATE);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
	    	.setTitle(R.string.date_time_title)
	    	.setItems(R.array.date_time_array, new DialogInterface.OnClickListener() {
	    		public void onClick(DialogInterface dialog, int which) {
					FragmentManager fm = getActivity().getSupportFragmentManager();
	    			if (which==0){
	    				DatePickerFragment dp = new DatePickerFragment().newInstance(mDate);
	    				dp.setTargetFragment(getTargetFragment(), REQUEST_DATE);
	    				dp.show(fm, DIALOG_DATE);
	    			}else{
	    				TimePickerFragment tp = new TimePickerFragment().newInstance(mDate);
	    				tp.setTargetFragment(getTargetFragment(), REQUEST_TIME);
	    				tp.show(fm, DIALOG_TIME);
	    			}
	    		}
	               
	        });
	           return builder.create();
	}
}
...

Now, on both DatePickerFragment and TimePickerFragment, I’m sending the positive button with the int value either sendResult(1) or sendResult(Activity.RESULT_OK) which is equal to -1 to the CrimeFragment.java, so its onActivityResult will update the date or the time.

...
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		if (requestCode == REQUEST_DATETIME){
			Date date;
			if(resultCode==1){
				date = (Date)data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
			}else{
				date = (Date)data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
			}
			mCrime.setDate(date);
			updateDate();
		}
	}

I don’t know if what I’m doing is the correct way on doing this. I couldn’t think other way of resolving it.
If not, I would appreciate if you guys may give me a hand by letting me know which would be the best way of doing this exercise.

Thanks in Advance.


#2

The tricky part here is this: you have to make sure that your TimePickerFragment preserves the original date! This can be easy to wipe out.

If you can post your TimePickerFragment code, I can provide more specific advice.