How to transfer the formatted date string from my DatePickerFragment?

pepr picture pepr · Aug 13, 2013 · Viewed 7.3k times · Source

Following the official doc http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker.

I have used just the same code, and added only the result formatting in the onDateSet method:

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);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());

        // How to get the string from here to the caller?

    }
}

For testing, the caller activity just displays the TextView and calls the picker when the user touches the widget:

public class OrderHeadEditActivity extends Activity {

    private TextView mDTDelivery;
    ...

    @Override
    protected void onCreate(Bundle bundle) {
        ...
        mDTDelivery = (TextView) findViewById(R.id.order_head_view_dt_delivery);
        ...
        mDTDelivery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }
        });
    }
...
}

The date picker is displayed, and after setting the breakpoint at the place where the result is formatted to a string, I can see it works.

However, I do not know how to pass the string value back to the mtDTDelivery widget?

Answer

Raghunandan picture Raghunandan · Aug 13, 2013

As tyczj pointed out you can use the interface method or you could make datapicker class an inner class of your activity class in which case it should be static and you need a weakreference of the outerclass

Using interface.

Define a interface in DatePickerFragment. Implement the interface in your activity class and set the date to textview.

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

    TheListener listener;

public interface TheListener{
    public void returnDate(String date);
}

@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);
listener = (TheListener) getActivity(); 

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null) 
{
  listener.returnDate(formattedDate); 

}

}
}

MainActivity

public class MainActivity extends Activity implements DatePickerFragment.TheListener{

    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void returnDate(String date) {
        // TODO Auto-generated method stub
        tv.setText(date);
    }

}

Second way not sure if this is the best way. I would recommend the above interface method.

public class MainActivity extends Activity {//implements DatePickerFragment.TheListener{

    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

        private WeakReference<MainActivity> mActivity;

    @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);
    mActivity = new WeakReference<MainActivity>((MainActivity) getActivity());

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
    Calendar c = Calendar.getInstance();
    c.set(year, month, day);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDate = sdf.format(c.getTime());
      MainActivity target = mActivity.get(); 
      if (target != null) target.tv.setText(formattedDate);

    }
    }
}