Thursday 15 July 2010

java - how to use shared preferences with radio group -



java - how to use shared preferences with radio group -

i have 2 buttons in next , previous , question textview , answers alternative in radio button . have save radio buttons values in shared preferences on click next button , when phone call previous button gets shared preferences value next button , set radio button when phone call previous button.but after run application radio button value saves in shared preferences.but when phone call previous button not shared preferences value , not show radio button checked. can 1 help me here.thanks in advanced.

here activity code.

read_questions_xml_file(); button_previouse.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub previousquestioncalled(v); } }); button_next.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub nextquestioncalled(v); } }); public void nextquestioncalled(view view) { int id = options_radiogroup.getcheckedradiobuttonid(); system.out.println("id = " + id); if (id > 0) { options_radiogroup.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(radiogroup r_group, int checked_id) { // todo auto-generated method stub radiobutton radiobutton = (radiobutton)r_group. findviewbyid(checked_id); int checkedindex = r_group.indexofchild(radiobutton); system.out.println("checkedindex = " + checkedindex); sharedpreferences sharedpreferences = getsharedpreferences("rgroup_shared_pref", mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putint("check", checkedindex); editor.commit(); } }); //if animationforward == false if (animationforward) { storingresult(); boolean flag; if (animationforward) { flag = false; } else { flag = true; } animationforward = flag; } //return number of element in vector > 0 //-1 + 1 if (-1 + vectore_mquestiondatabasestructure.size() > staticclass.questionnumber) { if (staticclass.istest) { staticclass.resultoftest_out_of = 1 + staticclass.resultoftest_out_of; } staticclass.questionnumber = 1 + staticclass.questionnumber; rehitting(); view.clearanimation(); if (!staticclass.istest) { button_questionlimit.clearanimation(); } if (staticclass.istest) { button_questionlimit.clearanimation(); } return; } else { button_questionlimit.startanimation(manimation); return; } } else if (id == -1) { toast.maketext(this, "please select option", toast.length_long) .show(); } } public void previousquestioncalled(view view) { system.out.println("_________________in previous question____________"); sharedpreferences sharedpreferences = getsharedpreferences("my_shared_pref", mode_private); int savedradioindex = sharedpreferences.getint("check", 0); system.out.println("savedradioindex "+savedradioindex); radiobutton savedcheckedradiobutton = (radiobutton)options_radiogroup.getchildat(savedradioindex); savedcheckedradiobutton.setchecked(true); system.out.println("_________________in previous question____________"); if (!animationforward) { boolean flag; if (animationforward) { flag = false; } else { flag = true; } animationforward = flag; } if (staticclass.questionnumber > 0) { if (!staticclass.istest) { staticclass.questionnumber = -1 + staticclass.questionnumber; rehitting(); button_questionlimit.clearanimation(); } view.clearanimation(); } else { button_questionlimit.startanimation(manimation); } }

when press next button saved shared prefs value in = int checkedindex = r_group.indexofchild(radiobutton);

but when press previous button here i'm getting shared prefs value 0 or -1 , radio button unchecked.

here logcat info

10-08 16:47:54.007: i/system.out(459): id = 2131165205 10-08 16:47:54.007: i/system.out(459): strlimit = 2/12 10-08 16:47:54.007: i/system.out(459): ************************in next button****************** 10-08 16:47:54.016: i/system.out(459): checkedindex = 2 10-08 16:47:54.086: i/system.out(459): ************************in next button****************** 10-08 16:47:54.086: i/system.out(459): ************************in next button****************** 10-08 16:47:54.086: i/system.out(459): checkedindex = -1 10-08 16:47:54.096: i/system.out(459): ************************in next button****************** 10-08 16:48:06.756: i/system.out(459): _________________in previous question____________ 10-08 16:48:06.756: i/system.out(459): savedradioindex -1

after have checked xml file ddms not store value n .it store -1 value .and here log cat shows shared prefs value.

<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <int name="check" value="-1" /> </map>

in radiogroup, have create localinstance while trying save current selected selection in radiogroup. have created simplest illustration , can implement mentioned below :

private void saveradiochoice(){ sharedpreferences msharedpref = getsharedpreferences(my_pref_key,mode_private); sharedpreferences.editor editor = msharedpref.edit(); // initialize radiogroup while saving choices radiogroup localradiogroup = (radiogroup) findviewbyid(r.id.choices); editor.putint(my_choice_key, localradiogroup.indexofchild(findviewbyid(localradiogroup.getcheckedradiobuttonid()))); editor.apply(); }

after saving value, can retrieve follows

private void retrievechoices(){ sharedpreferences sharedpref = getsharedpreferences(my_pref_key,mode_private); int = sharedpref.getint(my_choice_key,-1); if( >= 0){ ((radiobutton) ((radiogroup)findviewbyid(r.id.mychoiceviewradio)).getchildat(i)).setchecked(true); } }

java android

No comments:

Post a Comment