2

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

}

}

Gishantha Darshana
  • 163
  • 1
  • 6
  • 17

1 Answers1

2

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • hi i am not using activity here. when the user press an item on navigation list it is only show this fragment class. how can i do this with only the fragment without activity – Gishantha Darshana Jan 21 '14 at 12:25
  • @GishanthaDarshana you can't have a fragment without activity. Fragment is hosted by a activity. SO communicate date to activity. then activity to fragment. MainActivity is the one that hosts the framgents. You will have a container you just add or replace framgents. – Raghunandan Jan 21 '14 at 12:26
  • can you please tell me how to do that.. i have no idea with fragments an all.. what i understood is fragment class inflate my layout. and i can handle click events in the fragment. i have done certain things in one fragment. it has a button , when it is pressed it calls some web service and do some tasks. can you explain more further please – Gishantha Darshana Jan 21 '14 at 12:29
  • @GishanthaDarshana the docs has a example. read it first and check this http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment – Raghunandan Jan 21 '14 at 12:32
  • yes sir... i have searched it everywhere and finally it works. and your guidence also helpful thank you so much... sorry for the delay . – Gishantha Darshana Jan 22 '14 at 11:59