在我的應用程序中,我使用日期選擇器爲用戶選擇日期。如果用戶選擇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