在我的应用程序中,我使用日期选择器为用户选择日期.所以我在我的应用程序中使用数据库,如果用户选择1个特定项目意味着我将获取与该项目相关的数据,我将在另一个屏幕中显示它,因为它将有1个开始日期,我必须将该日期作为日期选择器中的默认日期加载..但是它将当前日期作为默认日期加载而不是从数据库中提取的日期..请帮助我..提前谢谢.
我的代码:
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);
每当您想将现有日期更新为日期选择器时.
如果要在创建日期选择器时设置任何日期,则可以覆盖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;
}
}
希望它会对你有所帮助.如果你遇到任何问题,你可以问.