Reputation: 635
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;
}
};
Upvotes: 8
Views: 25303
Reputation: 179
DatePickerDialog has a default constructor for this.
public DatePickerDialog(Context context,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth) {
}
Upvotes: 0
Reputation: 3966
use
date_picker_dialog.updateDate(year, month - 1, date);
whenever you want to update an existing date into date picker.
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.
Upvotes: 10
Reputation: 21201
see mYear_1
, mMonth_1
, mDay_1
are the fields in the date picker.
initialize these values before calling showDialog(DATE_DIALOG_ID_1);
date picker dialog.
Usage==>
mYear_1 = 2011;
mMonth_1 = 07;
mDay_1 = 29;
showDialog(DATE_DIALOG_ID_1);
Don't use c = Calendar.getInstance();
like this.
Just intialize the values from the database to the these values
Upvotes: 0