problem with datePicker, title initialization is funky
So I have reached the end of my rope when it comes to this dang datePicker dialog that I am working with. I have made it through 7 different activities within my app and have not encountered a single problem until I reached the datePicker. My problem consists of two elements: 1) The dang title of my datePicker displays Wednesday, December 31, 0002 when called. The datePicker itself displays correctly, but not the title, and I have not altered the title one bit. When you change the date the title changes to the correct information except for.... 2) It appears as if the days of the week are off by one (for today it says Monday, February 22, 2011). So I am going to post my entire code within this activity and if anyone has any ideas any help is much appreciated. The issue is going to be in the onCreateDialog or onPrepareDialog methods. If i need to remove any of the other extraneous init methods I will do so, but I would like to verify that I am not causing problems anywhere else within this activity.
public class QuizSettingsActivity extends QuizActivity {
SharedPreferences mGameSettings;
static final int DATE_DIALOG_ID = 0;
static final int PASSWORD_DIALOG_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//Retrieve the shared preferences
mGameSettings = getSharedPreferences(GAME_PREFERENCES,
Context.MODE_PRIVATE);
//Initialize nickname entry
initNicknameEntry();
//Initialize email entry
initEmailEntry();
//Initialize the password chooser
initPasswordChooser();
//Initialize date picker
initDatePicker();
//Initialize spinner
initGenderSpinner();
//Initialize clear
initClear();
}//end of on Create
//Initialize clear
private void initClear() {
Button clear = (Button)findViewById(R.id.BTN_Clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText nickname =
(EditText)findViewById(R.id.ET_Nickname);
nickname.setText("");
EditText email =
(EditText)findViewById(R.id.ET_Email);
email.setText("");
TextView dob =
(TextView)findViewById(R.id.TV_DOB_Info);
dob.setText(R.string.settings_dob_not_set);
Editor editor = mGameSettings.edit();
editor.remove(GAME_PREFERENCES_DOB);
editor.remove(GAME_PREFERENCES_GENDER);
//editor.clear();
editor.commit();
}//End of onClick
});//end of on click listener
}//end of clear method
//Handles logging upon leaving settings screen
@Override
protected void onDestroy(){
Log.d(DEBUG_TAG, "SHARED PREFERENCES");
Log.d(DEBUG_TAG, "Nickname is: "
+ mGameSettings.getString(GAME_PREFERENCES_NI开发者_开发技巧CKNAME,
"Not Set"));
Log.d(DEBUG_TAG, "Email is: "
+ mGameSettings.getString(GAME_PREFERENCES_EMAIL, "Not
Set"));
Log.d(DEBUG_TAG, "Gender (U=0, M=1, F=2) is: " +
+ mGameSettings.getInt(GAME_PREFERENCES_GENDER, 0));
//Don't save password as of yet
Log.d(DEBUG_TAG, "Password is: "
+ mGameSettings.getString(GAME_PREFERENCES_PASSWORD,
"Not Set"));
//Don't save dob yet
Log.d(DEBUG_TAG, "DOB is: "
+ DateFormat.format("MMMM, dd, yyyy",
mGameSettings.getLong(GAME_PREFERENCES_DOB,
0)));
super.onDestroy();
}//end of on Destroy
//Initialize spinner
private void initGenderSpinner() {
//Configuring spinner controls...load spinner
final Spinner spinner = (Spinner)
findViewById(R.id.Spinner_Gender);
ArrayAdapter<?> adapter =
ArrayAdapter.createFromResource(this,
R.array.gender, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if(mGameSettings.contains(GAME_PREFERENCES_GENDER)){
spinner.setSelection(mGameSettings.getInt(GAME_PREFERENCES_GENDER,
0));
}
//Handle spinner selection
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View
itemSelected,
int selectedItemPosition, long selectedId){
Editor editor = mGameSettings.edit();
editor.putInt(GAME_PREFERENCES_GENDER,
selectedItemPosition);
editor.commit();
}
public void onNothingSelected(AdapterView<?> parent){
}
});
}//end of spinner
//Initialize Date Picker
private void initDatePicker() {
//Set Date Info
TextView dobInfo = (TextView) findViewById(R.id.TV_DOB_Info);
if(mGameSettings.contains(GAME_PREFERENCES_DOB)){
dobInfo.setText(DateFormat.format("MMMM dd, yyyy",
mGameSettings.getLong(GAME_PREFERENCES_DOB, 0)));
}else {
dobInfo.setText(R.string.settings_dob_not_set);
}
//Handle date picking dialog
Button pickDate = (Button) findViewById(R.id.Button_DOB);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
/*Toast.makeText(QuizSettingsActivity.this,
"TODO: Launch DatePicker Dialog",
Toast.LENGTH_LONG).show();*/
}//end of onClick
});//end of OnClick Listener
}//end of date picker
@Override
protected Dialog onCreateDialog(int id){
switch(id){
case DATE_DIALOG_ID:
final TextView dob =
(TextView)findViewById(R.id.TV_DOB_Info);
DatePickerDialog dateDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view,
int year, int monthOfYear, int dayOfMonth)
{
Time dateOfBirth = new Time();
dateOfBirth.set(dayOfMonth, monthOfYear,
year);
long dtDob = dateOfBirth.toMillis(true);
dob.setText(DateFormat.format("MMMM dd,
yyyy", dtDob));
Editor editor = mGameSettings.edit();
editor.putLong(GAME_PREFERENCES_DOB,
dtDob);
editor.commit();
}//end of onDateSet
},0,0,0);
return dateDialog;
case PASSWORD_DIALOG_ID:
//build dialog inflate/load
LayoutInflater inflater =
(LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
final View layout =
inflater.inflate(R.layout.password_dialog,
(ViewGroup)findViewById(R.id.root));
final EditText p1 =
(EditText)layout.findViewById(R.id.EditText_Pwd1);
final EditText p2 =
(EditText)layout.findViewById(R.id.EditText_Pwd2);
final TextView error = (TextView)
layout.findViewById(R.id.TextView_PwdProblem);
p2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {//adding
import adds beforeTextChanged
//&
onTextChanged methods
String strPass1 = p1.getText().toString();
String strPass2 = p2.getText().toString();
if (strPass1.equals(strPass2)) {
error.setText(R.string.settings_pwd_equal);
} else {
error.setText(R.string.settings_pwd_not_equal);
}
}
@Override//added with import of afterTextChanged
public void beforeTextChanged(CharSequence s, int
start,
int count, int after) {
// TODO Auto-generated method stub
}
@Override//added with import of afterTextChanged
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
});//end of text changed listener
AlertDialog.Builder builder = new
AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle(R.string.settings_button_pwd);
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
whichButton) {
// dismiss and remove the Dialog, so it
// cannot be used again (no cached info)
QuizSettingsActivity.this.removeDialog(PASSWORD_DIALOG_ID);
}//end of onClick
});//end of on click listener
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
TextView passwordInfo =
(TextView)
findViewById(R.id.TV_Password_Info);
String strPassword1 = p1.getText().toString();
String strPassword2 = p2.getText().toString();
if (strPassword1.equals(strPassword2)) {
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_PASSWORD,
strPassword1);
editor.commit();
passwordInfo.setText(R.string.settings_pwd_set);
} else {
Log.d(DEBUG_TAG, "Passwords do not match. Not
saving. Keeping old password (if set).");
}
//dismiss and remove the dialog so it
// cannot be used again using removeDialog
QuizSettingsActivity.this.removeDialog(PASSWORD_DIALOG_ID);
}//end of onClick
});//end of onClick listener
//create and call dialog and return it
AlertDialog passwordDialog = builder.create();
return passwordDialog;
}//end of switch
return null;
}//end of Dialog on CreateDialog method
@Override
protected void onPrepareDialog(int id, Dialog dialog){
super.onPrepareDialog(id, dialog);
switch(id){
case DATE_DIALOG_ID:
//Handle any date picker dialog initialization here
DatePickerDialog dateDialog = (DatePickerDialog) dialog;
int iDay, iMonth, iYear;
//check for date of birth preference
if(mGameSettings.contains(GAME_PREFERENCES_DOB)){
//retrieve dob setting from preferences
long msBirthDate =
mGameSettings.getLong(GAME_PREFERENCES_DOB, 0);
Time dateOfBirth = new Time();
dateOfBirth.set(msBirthDate);
iDay = dateOfBirth.monthDay;
iMonth = dateOfBirth.month;
iYear = dateOfBirth.year;
} else {
final Calendar cal = Calendar.getInstance();
//Today's date fields
iDay = cal.get(Calendar.DAY_OF_MONTH);
iMonth = cal.get(Calendar.MONTH);
iYear = cal.get(Calendar.YEAR);
//dateDialog.setTitle("Birthday");
//dateDialog.updateDate(2000,11,25);
}
//set the date in the date picker to the date of birth
//OR to the current date
dateDialog.updateDate(iYear, iMonth, iDay);
return;
case PASSWORD_DIALOG_ID:
return;
}//end of switch
}//end of onPrepareDialog method
//Initialize Password Chooser
private void initPasswordChooser() {
// Set password info
TextView passwordInfo = (TextView)
findViewById(R.id.TV_Password_Info);
if(mGameSettings.contains(GAME_PREFERENCES_PASSWORD)){
passwordInfo.setText(R.string.set_password);
}else{
passwordInfo.setText(R.string.settings_pwd_not_set);
}
//Handle password setting dialog
Button setPassword = (Button)
findViewById(R.id.Button_Password);
setPassword.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(PASSWORD_DIALOG_ID);
/*Toast.makeText(QuizSettingsActivity.this, "TODO:
Launch Password Dialog",
Toast.LENGTH_LONG).show();*/
}
});
}//End of Password Chooser
//Initialize Email entry
private void initEmailEntry() {
// Save Email
final EditText emailText = (EditText)
findViewById(R.id.ET_Email);
if (mGameSettings.contains(GAME_PREFERENCES_EMAIL)){
emailText.setText(mGameSettings.getString(GAME_PREFERENCES_EMAIL,
""));
}
emailText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((event.getAction() == KeyEvent.ACTION_DOWN ||
event.getAction() == KeyEvent.ACTION_UP) &&
(keyCode == KeyEvent.KEYCODE_ENTER || keyCode ==
KeyEvent.KEYCODE_TAB)){
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_EMAIL,
emailText.getText().toString());
editor.commit();
return true;
}
return false;
}
});
}//end of email
//Initialize Nickname entry
private void initNicknameEntry() {
// Save Nickname
final EditText nicknameText = (EditText)
findViewById(R.id.ET_Nickname);
if (mGameSettings.contains(GAME_PREFERENCES_NICKNAME)){
nicknameText.setText(mGameSettings.getString(GAME_PREFERENCES_NICKNAME,
""));
}
nicknameText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((event.getAction() == KeyEvent.ACTION_DOWN ||
event.getAction() == KeyEvent.ACTION_UP) &&
(keyCode == KeyEvent.KEYCODE_ENTER || keyCode ==
KeyEvent.KEYCODE_TAB)){
String strNickname =
nicknameText.getText().toString();
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_NICKNAME,
strNickname);
editor.commit();
return true;
}
return false;
}
});
}//End of nickname
}//end of QuizSettingsActivity
I have also tried to setTitle, but that only works up until the date is changed.
FROSTYSMOOTH,
I believe what you are seeing is an incompatibility between the Android Time
and Calendar
data types you are using.
The ENUM values for the days of the week are inconsistent between them...
Monday in a Calendar
object has a value of 0x2 SEE HERE.
Monday in a Time
object has a value of 0x1 SEE HERE.
So when you try to set a Calendar
object with values from a Time
object, you will be a day off.
You need to sort out when each is being used an adjust accordingly or stick to a single Date/Time object so you won't run into these issues.
I hope I was able to help, good luck!
精彩评论