Gishantha Darshana
Gishantha Darshana

Reputation: 163

date picker with fragment class android

i am developing an application with the navigation drawer. when i click some items in the navigation list it shows some interface using a fragment. and my click items also working properly. i have use an image button to show a date picker. i have use below codes. but when i set an date from data picker dialog. it does not change the given text view. please guide me with some code samples..

public class FindPeopleFragment extends DialogFragment  implements DatePickerDialog.OnDateSetListener  {

    public int year;
    public int month;
    public int day;
    static final int DATE_PICKER_ID = 1111; 
    TextView curentDate;




public FindPeopleFragment(TextView date){
    curentDate = date;
}
public FindPeopleFragment(){

}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.schedule, container, false);
    ImageButton date = (ImageButton) rootView.findViewById(R.id.btnDate); 
     curentDate = (TextView) rootView.findViewById(R.id.txtCurrentDate);

     /*final Calendar c = Calendar.getInstance();
     year  = c.get(Calendar.YEAR);
     month = c.get(Calendar.MONTH);
     day   = c.get(Calendar.DAY_OF_MONTH);

    curentDate.setText(new StringBuilder()
    // Month is 0 based, just add 1
    .append(month + 1).append("-").append(day).append("-")
    .append(year).append(" "));*/


    date.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Toast.makeText(getActivity(), "clicked", Toast.LENGTH_LONG).show();
            //showDialog(DATE_PICKER_ID);
            showDatePickerDialog(v);
        }
    });
    return rootView;


}

public Dialog onCreateDialog(Bundle savedInstanceState) {


    final Calendar c = Calendar.getInstance();
    year  = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day   = c.get(Calendar.DAY_OF_MONTH);
    return new DatePickerDialog(getActivity(), datePickerListener, year, month, day);

}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener(){
public void onDateSet(DatePicker view, int syear, int smonth, int sday) {
    // Do something with the date chosen by the user
    month = smonth;
    day = sday;
    year = syear;
    curentDate.setText(String.valueOf(month + 1 ) + "/" +   String.valueOf(day) + "/" + String.valueOf(year));

}

};

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

}

Upvotes: 2

Views: 2567

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Creating a constructor with params is a bad idea.

Use a interface as a call back to the activity and set the date selected to textview in activity or framgent.

From DialogFragment to Activity

How to transfer the formatted date string from my DatePickerFragment?

Activity to Fragment

Send data from activity to fragment in android

DialogFragment-->Activity-->Fragment

http://developer.android.com/training/basics/fragments/communicating.html

Upvotes: 2

Related Questions