how to enable particular radio button of radio group according to radio button id saved in shared preference in android studio -
i developing quiz app. in there 2 buttons previous question , next question.what want when user selects answer options (for options there 4 radio buttons) , attempts next question if user wants view previous question time same radio button should enabled ever selected user. used shared preferences storing selected radio button id. here code: next button click:
nextbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int radioselected = radiogroup.getcheckedradiobuttonid(); int userselection = getselectedanswer(radioselected); sharedpreferences=getsharedpreferences(mypreferences,mode_private); sharedpreferences.editor editor=sharedpreferences.edit(); editor.putint("ans",userselection); editor.commit(); int correctanswerforquestion =firstquestion.getcorrectanswer(); if(userselection == correctanswerforquestion) { toast.maketext(getapplicationcontext(),"correct answer",toast.length_long).show(); currentquizquestion++; if(currentquizquestion>=quizcount) { toast.maketext(getapplicationcontext(),"end of quiz",toast.length_long).show(); return; } else { firstquestion=parsedobject.get(currentquizquestion); txtquestion.settext(firstquestion.getquestion()); string[] possibleanswers=firstquestion.getanswers().split(","); uncheckedradiobutton(); optionone.settext(possibleanswers[0]); optiontwo.settext(possibleanswers[1]); optionthree.settext(possibleanswers[2]); optionfour.settext(possibleanswers[3]); } } else { toast.maketext(getapplicationcontext(),"you choose wrong answer",toast.length_long).show(); currentquizquestion++; firstquestion=parsedobject.get(currentquizquestion); txtquestion.settext(firstquestion.getquestion()); string[] possibleanswers=firstquestion.getanswers().split(","); uncheckedradiobutton(); optionone.settext(possibleanswers[0]); optiontwo.settext(possibleanswers[1]); optionthree.settext(possibleanswers[2]); optionfour.settext(possibleanswers[3]); } } });
and previous button:
previousbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { currentquizquestion--; if(currentquizquestion<0) { return; } checkedradiobutton(); firstquestion=parsedobject.get(currentquizquestion); txtquestion.settext(firstquestion.getquestion()); string[] possibleanswers=firstquestion.getanswers().split(","); optionone.settext(possibleanswers[0]); optiontwo.settext(possibleanswers[1]); optionthree.settext(possibleanswers[2]); optionfour.settext(possibleanswers[3]); } }); json_data_web_call(); } private void checkedradiobutton() { int ans=1; int ans1=sharedpreferences.getint("ans",ans); if(optionone.getid()==ans1) { optionone.setchecked(true); } }
please me. lot.
add tag each radio button option optionone.settag("answer_tag", "one"). store selected tag, , compare radiobuttons.gettag("answer_tag) stored tag.
Comments
Post a Comment