Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last selected date is not getting in datepicker dialog

In my app I have one datepicker, I am able to select and set the selected date in textview, but the issue is if again I click on textview to open datepicker dialog, it always shows current date instead of last selected date..so what is the issue?

  public class MainActivity extends Activity {

private TextView date_dropdown;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    date_dropdown=(TextView)findViewById(R.id.shows_dt);
    
    
    Calendar calendar = Calendar.getInstance();

    date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
            + (calendar.get(Calendar.MONTH) + 1) + "-"
            + calendar.get(Calendar.YEAR));

    date_dropdown.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDatePickerDialog();
        }
    });
}

public void showDatePickerDialog() {

    System.out.println("show date picke dilg ");
    System.out.println("show date picke dilg");

    DialogFragment newFragment1 = new SelectDateFragment();
    newFragment1.show(getFragmentManager(), "DatePicker");
}

public class SelectDateFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm + 1, dd);
    }

    public void populateSetDate(int year, int month, int day) {
        date_dropdown.setText(day + "-" + month + "-" + year);
    }

}
like image 551
Aditya Avatar asked Jun 04 '15 13:06

Aditya


2 Answers

Below Line You Have Given get only Current date, so You will Get current date only.

 date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
        + (calendar.get(Calendar.MONTH) + 1) + "-"
        + calendar.get(Calendar.YEAR));

save your Selected date in Shared Preference then Show in your TextView

like image 99
Mano Avatar answered Oct 05 '22 23:10

Mano


You should define calender instance in onCreate() method and then all year month and date value update onDateSet(), So when you again open date picker dialog it will surly display previously selected date.

Do just like this

  public class MainActivity extends Activity {

private TextView date_dropdown;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


       final Calendar calendar = Calendar.getInstance();
        int yy1 = calendar.get(Calendar.YEAR);
        int mm1 = calendar.get(Calendar.MONTH);
        int dd1 = calendar.get(Calendar.DAY_OF_MONTH);

    date_dropdown=(TextView)findViewById(R.id.shows_dt);


    Calendar calendar = Calendar.getInstance();

    date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
            + (calendar.get(Calendar.MONTH) + 1) + "-"
            + calendar.get(Calendar.YEAR));

    date_dropdown.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDatePickerDialog();
        }
    });
}

public void showDatePickerDialog() {

    System.out.println("show date picke dilg ");
    System.out.println("show date picke dilg");

    DialogFragment newFragment1 = new SelectDateFragment();
    newFragment1.show(getFragmentManager(), "DatePicker");
}

public class SelectDateFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new DatePickerDialog(getActivity(), this, yy1, mm1, dd1);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm + 1, dd);

         yy1 = yy ;
         mm1 = mm;
         dd1 = dd ;
    }

    public void populateSetDate(int year, int month, int day) {
        date_dropdown.setText(day + "-" + month + "-" + year);
    }

}
like image 22
Meritorious Infotech Avatar answered Oct 06 '22 01:10

Meritorious Infotech