1. Download our Official Android App: Forums for Android!

Apps Problem with datePicker

Discussion in 'Android Development' started by FROSTYSMOOTH, Feb 22, 2011.

  1. FROSTYSMOOTH

    FROSTYSMOOTH Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Feb 22, 2011

    Feb 22, 2011
    1
    0
    5
    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.

    Code (Text):
    1.  
    2.  
    3.  
    4.  
    5. public class QuizSettingsActivity extends QuizActivity {
    6.     SharedPreferences mGameSettings;
    7.     static final int DATE_DIALOG_ID = 0;
    8.     static final int PASSWORD_DIALOG_ID = 1;
    9.    
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.settings);
    15.        
    16.         //Retrieve the shared preferences
    17.         mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
    18.         //Initialize nickname entry
    19.         initNicknameEntry();
    20.         //Initialize email entry
    21.         initEmailEntry();
    22.         //Initialize the password chooser
    23.         initPasswordChooser();
    24.         //Initialize date picker
    25.         initDatePicker();
    26.         //Initialize spinner
    27.         initGenderSpinner();
    28.         //Initialize clear
    29.         initClear();
    30.        
    31.     }//end of on Create
    32.    
    33.    
    34.     //Initialize clear
    35.     private void initClear() {
    36.         Button clear = (Button)findViewById(R.id.BTN_Clear);
    37.         clear.setOnClickListener(new View.OnClickListener() {
    38.             public void onClick(View v) {
    39.                 EditText nickname = (EditText)findViewById(R.id.ET_Nickname);
    40.                 nickname.setText("");
    41.                 EditText email = (EditText)findViewById(R.id.ET_Email);
    42.                 email.setText("");
    43.                 TextView dob = (TextView)findViewById(R.id.TV_DOB_Info);
    44.                 dob.setText(R.string.settings_dob_not_set);                
    45.                
    46.                 Editor editor = mGameSettings.edit();
    47.                 editor.remove(GAME_PREFERENCES_DOB);
    48.                 editor.remove(GAME_PREFERENCES_GENDER);
    49.                 //editor.clear();
    50.                 editor.commit();    
    51.             }//End of onClick
    52.         });//end of on click listener        
    53.     }//end of clear method
    54.    
    55.  
    56.     //Handles logging upon leaving settings screen
    57.     @Override
    58.     protected void onDestroy(){
    59.         Log.d(DEBUG_TAG, "SHARED PREFERENCES");
    60.         Log.d(DEBUG_TAG, "Nickname is:  "
    61.                 + mGameSettings.getString(GAME_PREFERENCES_NICKNAME, "Not Set"));
    62.         Log.d(DEBUG_TAG, "Email is:  "
    63.                 + mGameSettings.getString(GAME_PREFERENCES_EMAIL, "Not Set"));
    64.         Log.d(DEBUG_TAG, "Gender (U=0, M=1, F=2) is:  " +
    65.                 + mGameSettings.getInt(GAME_PREFERENCES_GENDER, 0));
    66.         //Don't save password as of yet
    67.         Log.d(DEBUG_TAG, "Password is:  "  
    68.                 + mGameSettings.getString(GAME_PREFERENCES_PASSWORD, "Not Set"));
    69.         //Don't save dob yet
    70.         Log.d(DEBUG_TAG, "DOB is:  "
    71.                 + DateFormat.format("MMMM, dd, yyyy",
    72.                         mGameSettings.getLong(GAME_PREFERENCES_DOB, 0)));
    73.         super.onDestroy();        
    74.     }//end of on Destroy
    75.    
    76.    
    77.    
    78.    
    79.     //Initialize spinner
    80.     private void initGenderSpinner() {
    81.          //Configuring spinner controls...load spinner
    82.         final Spinner spinner = (Spinner) findViewById(R.id.Spinner_Gender);
    83.         ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,
    84.                 R.array.gender, android.R.layout.simple_spinner_item);
    85.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    86.         spinner.setAdapter(adapter);
    87.         if(mGameSettings.contains(GAME_PREFERENCES_GENDER)){
    88.             spinner.setSelection(mGameSettings.getInt(GAME_PREFERENCES_GENDER, 0));
    89.         }
    90.         //Handle spinner selection
    91.         spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    92.             public void onItemSelected(AdapterView<?> parent, View itemSelected,
    93.                     int selectedItemPosition, long selectedId){
    94.                 Editor editor = mGameSettings.edit();
    95.                 editor.putInt(GAME_PREFERENCES_GENDER, selectedItemPosition);
    96.                 editor.commit();
    97.             }
    98.            
    99.             public void onNothingSelected(AdapterView<?> parent){
    100.             }
    101.         });
    102.     }//end of spinner
    103.    
    104.    
    105.     //Initialize Date Picker
    106.     private void initDatePicker() {
    107.         //Set Date Info
    108.         TextView dobInfo = (TextView) findViewById(R.id.TV_DOB_Info);
    109.         if(mGameSettings.contains(GAME_PREFERENCES_DOB)){
    110.             dobInfo.setText(DateFormat.format("MMMM dd, yyyy",
    111.                     mGameSettings.getLong(GAME_PREFERENCES_DOB, 0)));
    112.         }else {
    113.             dobInfo.setText(R.string.settings_dob_not_set);
    114.         }
    115.         //Handle date picking dialog
    116.         Button pickDate = (Button) findViewById(R.id.Button_DOB);
    117.         pickDate.setOnClickListener(new View.OnClickListener() {
    118.             public void onClick(View v) {
    119.                 showDialog(DATE_DIALOG_ID);
    120.                 /*Toast.makeText(QuizSettingsActivity.this,
    121.                         "TODO: Launch DatePicker Dialog", Toast.LENGTH_LONG).show();*/
    122.             }//end of onClick
    123.         });//end of OnClick Listener
    124.     }//end of date picker
    125.    
    126.    
    127.     @Override
    128.     protected Dialog onCreateDialog(int id){
    129.         switch(id){
    130.         case DATE_DIALOG_ID:
    131.             final TextView dob = (TextView)findViewById(R.id.TV_DOB_Info);
    132.             DatePickerDialog dateDialog = new DatePickerDialog(this,
    133.                     new DatePickerDialog.OnDateSetListener() {
    134.                         public void onDateSet(DatePicker view,
    135.                             int year, int monthOfYear, int dayOfMonth) {
    136.                             Time dateOfBirth = new Time();
    137.                             dateOfBirth.set(dayOfMonth, monthOfYear, year);
    138.                             long dtDob = dateOfBirth.toMillis(true);
    139.                             dob.setText(DateFormat.format("MMMM dd, yyyy", dtDob));
    140.                            
    141.                             Editor editor = mGameSettings.edit();
    142.                             editor.putLong(GAME_PREFERENCES_DOB, dtDob);
    143.                             editor.commit();                    
    144.                         }//end of onDateSet
    145.             },0,0,0);
    146.             return dateDialog;
    147.            
    148.         case PASSWORD_DIALOG_ID:
    149.             //build dialog inflate/load
    150.             LayoutInflater inflater = (LayoutInflater)getSystemService(
    151.                     Context.LAYOUT_INFLATER_SERVICE);
    152.             final View layout = inflater.inflate(R.layout.password_dialog,
    153.                     (ViewGroup)findViewById(R.id.root));
    154.             final EditText p1 = (EditText)layout.findViewById(R.id.EditText_Pwd1);
    155.             final EditText p2 = (EditText)layout.findViewById(R.id.EditText_Pwd2);
    156.             final TextView error = (TextView) layout.findViewById(R.id.TextView_PwdProblem);
    157.             p2.addTextChangedListener(new TextWatcher() {
    158.                 @Override
    159.                 public void afterTextChanged(Editable s) {//adding import adds beforeTextChanged
    160.                                                           //& onTextChanged methods
    161.                     String strPass1 = p1.getText().toString();
    162.                     String strPass2 = p2.getText().toString();
    163.                     if (strPass1.equals(strPass2)) {
    164.                         error.setText(R.string.settings_pwd_equal);
    165.                     } else {
    166.                         error.setText(R.string.settings_pwd_not_equal);
    167.                     }
    168.                 }
    169.  
    170.                 @Override//added with import of afterTextChanged
    171.                 public void beforeTextChanged(CharSequence s, int start,
    172.                         int count, int after) {
    173.                     // TODO Auto-generated method stub
    174.                    
    175.                 }
    176.  
    177.                 @Override//added with import of afterTextChanged
    178.                 public void onTextChanged(CharSequence s, int start,
    179.                         int before, int count) {
    180.                     // TODO Auto-generated method stub
    181.                    
    182.                 }
    183.             });//end of text changed listener
    184.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
    185.             builder.setView(layout);
    186.             builder.setTitle(R.string.settings_button_pwd);
    187.             builder.setNegativeButton(android.R.string.cancel,
    188.                     new DialogInterface.OnClickListener() {
    189.                 public void onClick(DialogInterface dialog, int whichButton) {
    190.                     // dismiss and remove the Dialog, so it
    191.                     // cannot be used again (no cached info)
    192.                     QuizSettingsActivity.this.removeDialog(PASSWORD_DIALOG_ID);
    193.                 }//end of onClick
    194.             });//end of on click listener
    195.            
    196.             builder.setPositiveButton(android.R.string.ok,
    197.                     new DialogInterface.OnClickListener() {
    198.                 public void onClick(DialogInterface dialog, int which) {
    199.                     TextView passwordInfo =
    200.                         (TextView) findViewById(R.id.TV_Password_Info);
    201.                     String strPassword1 = p1.getText().toString();
    202.                     String strPassword2 = p2.getText().toString();
    203.                     if (strPassword1.equals(strPassword2)) {
    204.                         Editor editor = mGameSettings.edit();
    205.                         editor.putString(GAME_PREFERENCES_PASSWORD, strPassword1);
    206.                         editor.commit();
    207.                         passwordInfo.setText(R.string.settings_pwd_set);
    208.                     } else {
    209.                         Log.d(DEBUG_TAG, "Passwords do not match. Not saving. Keeping old password (if set).");
    210.                     }
    211.                     //dismiss and remove the dialog so it
    212.                     // cannot be used again using removeDialog
    213.                     QuizSettingsActivity.this.removeDialog(PASSWORD_DIALOG_ID);
    214.                 }//end of onClick
    215.             });//end of onClick listener
    216.             //create and call dialog and return it
    217.             AlertDialog passwordDialog = builder.create();
    218.             return passwordDialog;
    219.         }//end of switch
    220.         return null;    
    221.     }//end of Dialog on CreateDialog method
    222.    
    223.    
    224.    
    225.     @Override
    226.     protected void onPrepareDialog(int id, Dialog dialog){
    227.         super.onPrepareDialog(id, dialog);        
    228.         switch(id){
    229.         case DATE_DIALOG_ID:
    230.             //Handle any date picker dialog initialization here
    231.             DatePickerDialog dateDialog = (DatePickerDialog) dialog;
    232.             int iDay, iMonth, iYear;
    233.             //check for date of birth preference
    234.             if(mGameSettings.contains(GAME_PREFERENCES_DOB)){
    235.                 //retrieve dob setting from preferences
    236.                 long msBirthDate = mGameSettings.getLong(GAME_PREFERENCES_DOB, 0);
    237.                 Time dateOfBirth = new Time();
    238.                 dateOfBirth.set(msBirthDate);
    239.                 iDay = dateOfBirth.monthDay;
    240.                 iMonth = dateOfBirth.month;
    241.                 iYear = dateOfBirth.year;
    242.             } else {
    243.                 final Calendar cal = Calendar.getInstance();
    244.                 //Today's date fields
    245.                 iDay = cal.get(Calendar.DAY_OF_MONTH);
    246.                 iMonth = cal.get(Calendar.MONTH);
    247.                 iYear = cal.get(Calendar.YEAR);
    248.                
    249.                 //dateDialog.setTitle("Birthday");                
    250.                 //dateDialog.updateDate(2000,11,25);
    251.                
    252.             }
    253.             //set the date in the date picker to the date of birth
    254.             //OR to the current date            
    255.             dateDialog.updateDate(iYear, iMonth, iDay);
    256.             return;
    257.            
    258.         case PASSWORD_DIALOG_ID:
    259.             return;
    260.         }//end of switch
    261.     }//end of onPrepareDialog method
    262.    
    263.  
    264.     //Initialize Password Chooser
    265.     private void initPasswordChooser() {
    266.         // Set password info
    267.         TextView passwordInfo = (TextView) findViewById(R.id.TV_Password_Info);
    268.         if(mGameSettings.contains(GAME_PREFERENCES_PASSWORD)){
    269.             passwordInfo.setText(R.string.set_password);
    270.         }else{
    271.             passwordInfo.setText(R.string.settings_pwd_not_set);
    272.         }
    273.         //Handle password setting dialog
    274.         Button setPassword = (Button) findViewById(R.id.Button_Password);
    275.         setPassword.setOnClickListener(new View.OnClickListener() {
    276.             public void onClick(View v) {
    277.                 showDialog(PASSWORD_DIALOG_ID);
    278.                 /*Toast.makeText(QuizSettingsActivity.this, "TODO: Launch Password Dialog",
    279.                         Toast.LENGTH_LONG).show();*/                
    280.             }
    281.         });
    282.     }//End of Password Chooser
    283.    
    284.    
    285.     //Initialize Email entry
    286.     private void initEmailEntry() {
    287.         // Save Email
    288.         final EditText emailText = (EditText) findViewById(R.id.ET_Email);
    289.         if (mGameSettings.contains(GAME_PREFERENCES_EMAIL)){
    290.             emailText.setText(mGameSettings.getString(GAME_PREFERENCES_EMAIL, ""));
    291.         }
    292.         emailText.setOnKeyListener(new View.OnKeyListener() {
    293.         public boolean onKey(View v, int keyCode, KeyEvent event) {
    294.             if((event.getAction() == KeyEvent.ACTION_DOWN ||
    295.                     event.getAction() == KeyEvent.ACTION_UP) &&
    296.                     (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_TAB)){
    297.                 Editor editor = mGameSettings.edit();
    298.                 editor.putString(GAME_PREFERENCES_EMAIL, emailText.getText().toString());
    299.                 editor.commit();
    300.                 return true;
    301.             }
    302.             return false;
    303.         }
    304.         });        
    305.     }//end of email
    306.  
    307.    
    308.     //Initialize Nickname entry
    309.     private void initNicknameEntry() {
    310.         // Save Nickname
    311.         final EditText nicknameText = (EditText) findViewById(R.id.ET_Nickname);
    312.         if (mGameSettings.contains(GAME_PREFERENCES_NICKNAME)){
    313.             nicknameText.setText(mGameSettings.getString(GAME_PREFERENCES_NICKNAME, ""));
    314.         }
    315.         nicknameText.setOnKeyListener(new View.OnKeyListener() {
    316.         public boolean onKey(View v, int keyCode, KeyEvent event) {
    317.             if((event.getAction() == KeyEvent.ACTION_DOWN ||
    318.                     event.getAction() == KeyEvent.ACTION_UP) &&
    319.                     (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_TAB)){
    320.                 String strNickname = nicknameText.getText().toString();
    321.                 Editor editor = mGameSettings.edit();
    322.                 editor.putString(GAME_PREFERENCES_NICKNAME, strNickname);
    323.                 editor.commit();
    324.                 return true;
    325.             }
    326.             return false;
    327.         }
    328.         });
    329.     }//End of nickname
    330.    
    331. }//end of QuizSettingsActivity
    332.  
    333.  
     

    Advertisement

Share This Page

Loading...