在我的应用程序中,我使用日期选择器为用户选择日期。如果用户选择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;
}
};
检查从你的数据库中传出什么日期,因为我执行了你的程序并且它返回了我正确的日期。 – Android 2012-08-13 05:35:22