带有默认日期的日期选择器 [英] Date picker with default date

查看:31
本文介绍了带有默认日期的日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用日期选择器让用户选择日期.此外,我在我的应用程序中使用数据库,如果用户选择 1 个特定项目,则意味着我将获取与该项目相关的数据,并将其显示在另一个屏幕上,因为它将有 1 个开始日期,我必须将该日期加载为日期选择器中的默认日期..

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.

我的代码:

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

推荐答案

使用

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

每当您想将现有日期更新到日期选择器时.

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

如果你想在创建日期选择器时设置任何日期,那么你可以覆盖 oncreatedialog

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.

这篇关于带有默认日期的日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆