7

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;
                    }
            };
4

3 回答 3

10

利用

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

每当您想将现有日期更新为日期选择器时。

如果您想在创建日期选择器时设置任何日期,那么您可以覆盖 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;
        }
    }

希望它会帮助你。如果您遇到任何问题,那么您可以询问。

于 2012-08-13T05:41:21.173 回答
0

请参阅mYear_1, mMonth_1,mDay_1是日期选择器中的字段。

在调用showDialog(DATE_DIALOG_ID_1);日期选择器对话框之前初始化这些值。

用法==>

mYear_1     = 2011;
mMonth_1    = 07;
mDay_1      = 29;

showDialog(DATE_DIALOG_ID_1);

不要这样使用 c = Calendar.getInstance();

只需将数据库中的值初始化为这些值

于 2012-08-13T05:02:18.667 回答
0

DatePickerDialog 为此有一个默认构造函数。

public DatePickerDialog(Context context,
            OnDateSetListener callBack,
            int year,
            int monthOfYear,
            int dayOfMonth) {

    }
于 2016-07-26T11:12:10.843 回答