android - problem with datePicker, title initialization is funky -


so have reached end of rope when comes dang datepicker dialog working with. have made through 7 different activities within app , have not encountered single problem until reached datepicker. problem consists of 2 elements: 1) dang title of datepicker displays wednesday, december 31, 0002 when called. datepicker displays correctly, not title, , have not altered title 1 bit. when change date title changes correct information except for.... 2) appears if days of week off 1 (for today says monday, february 22, 2011). going post entire code within activity , if has ideas appreciated. issue going in oncreatedialog or onpreparedialog methods. if need remove of other extraneous init methods so, verify not causing problems anywhere else within activity.

public class quizsettingsactivity extends quizactivity {    sharedpreferences mgamesettings;    static final int date_dialog_id = 0;    static final int password_dialog_id = 1;     /** called when activity first created. */    @override    public void oncreate(bundle savedinstancestate) {        super.oncreate(savedinstancestate);        setcontentview(r.layout.settings);         //retrieve shared preferences        mgamesettings = getsharedpreferences(game_preferences, context.mode_private);        //initialize nickname entry        initnicknameentry();        //initialize email entry        initemailentry();        //initialize 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_nickname, "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 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 import of aftertextchanged                public void beforetextchanged(charsequence s, int start,                        int count, int after) {                    // todo auto-generated method stub                 }                 @override//added 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 , remove dialog,                    // cannot 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 not match. not saving. keeping old password (if set).");                    }                    //dismiss , remove dialog                    // cannot used again using removedialog  quizsettingsactivity.this.removedialog(password_dialog_id);                }//end of onclick            });//end of onclick listener            //create , call dialog , return            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 date picker dialog initialization here            datepickerdialog datedialog = (datepickerdialog) dialog;            int iday, imonth, iyear;            //check date of birth preference            if(mgamesettings.contains(game_preferences_dob)){                //retrieve dob setting 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 date in date picker date of birth            //or 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 tried settitle, works until date changed.

frostysmooth,

i believe seeing incompatibility between android time , calendar data types using.

the enum values days of week inconsistent between them...

monday in calendar object has value of 0x2 see here.

monday in time object has value of 0x1 see here.

so when try set calendar object values time object, day off.

you need sort out when each being used adjust accordingly or stick single date/time object won't run these issues.

i hope able help, luck!


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -