Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date picker with default date

In my application i am using date picker for user to select date.ALso i am using database in my application,if user selects 1 particular item means i will fetch data related to that item and i will show it in another screen,in that it will have 1 start date,i have to load that date as default date in date picker..But it is loading the current date as default date but not the date fetched from database..Please help me..Thanks in advance.

My code :

 c = Calendar.getInstance();               
               SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
               System.out.println("After format");
               Date pickdefdate=null;
//             String pickdefdatepar=null;
               try {
                   System.out.println("Inside try="+date);
              pickdefdate=sdf.parse(date);              ----------->date which is fetched from database.
              System.out.println("dddddddd="+pickdefdate);

              c.setTime(pickdefdate);    --------------------->Setting this date as current date..
              System.out.println("After parse");
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
               mYear = c.get(Calendar.YEAR);             ----------as u said i am setting this value before dialog.
               mMonth = c.get(Calendar.MONTH);
               mDay = c.get(Calendar.DAY_OF_MONTH);     
               showDialog(DATE_DIALOG_ID);



 protected Dialog onCreateDialog(int id) 
     {
            switch (id) {
            case DATE_DIALOG_ID:
                System.out.println("in dia="+mDay);
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
            }
            return null;
        }    

     private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        System.out.println("Inside dia");
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                    }
            };
like image 705
prakash .k Avatar asked Aug 13 '12 04:08

prakash .k


1 Answers

use

    date_picker_dialog.updateDate(year, month - 1, date);

whenever you want to update an existing date into date picker.

If you want to set any date while create date picker then you can override oncreatedialog

    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id) {
            case DATE_DIALOG_ID:
                date_picker_dialog = new DatePickerDialog(this, get_date, year, month - 1, date);
                return date_picker_dialog;
        }
    }

hope it will help you. If any problem your are facing then you can ask.

like image 129
Bharat Sharma Avatar answered Sep 22 '22 00:09

Bharat Sharma