keshav
keshav

Reputation: 13

android using 1 date picker for different textfields

android...how can i use a date picker when click on different text views to display then selected dates in the clicked text-view.. i tried switch and on-click listeners within success... only for 1 text view is working as follows

enter code here
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment() ;
    newFragment.show(getSupportFragmentManager(), "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) {
        // display selected date in s-fire

        TextView sfire = (TextView) findViewById(R.id.fire);

        Date d = new Date(year - 1900, month, day);
        String sDate = new SimpleDateFormat("dd/MM/yyyy").format(d);
        sfire.setText(sDate.toString());

        Log.d("hello", sDate.toString());
    }



}

Upvotes: 0

Views: 225

Answers (1)

user3262445
user3262445

Reputation:

Currently your date picker fragment will only work with the TextView that you're saving the data too:

TextView sfire = (TextView) findViewById(R.id.fire);

You could pass the id of the TextView into the fragment as an argument, then add the date to the specified id:

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment() ;
    Bundle args = new Bundle();
    args.putInt("id", R.id.fire);
    newFragment.show(getSupportFragmentManager(), "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) {
        // display selected date in s-fire

        TextView sfire = getArguments().getInt("id");    

        Date d = new Date(year - 1900, month, day);
        String sDate = new SimpleDateFormat("dd/MM/yyyy").format(d);
        sfire.setText(sDate.toString());

        Log.d("hello", sDate.toString());
    }

}

Upvotes: 2

Related Questions