restore date from sharedpreferences and show change in inline datepicker in android
I made an android app in which I am taking the date of birth of the user and storing them as three integers in SharedPreferences
key-value pairs.
The date is picked by using inline datepicker.
I want the date selected to be same when user开发者_Go百科 again opens it instead of showing today's date.
How do I use the Sharedpreferences
key-value pair data and show that in the inline date instead of today's date?
Hi I found the answer on how to set the inline datepicker here is the code
OnDateChangedListener listener = new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
}
};
date.init(prefs.getInt("year",1920),prefs.getInt("month",0),prefs.getInt("day",1),listener);
The mistake I did was not using a listener which led to force closing of the app
Look here for clues ... http://developer.android.com/resources/tutorials/views/hello-datepicker.html
I would think you could replace the Calendar.YEAR[.MONTH|.DAY_OF_MONTH] with your call to app preferences:
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
精彩评论