Sunday 15 March 2015

java - RadioGroup check doesn't work for me -



java - RadioGroup check doesn't work for me -

i have such functionality - have dialog (with settings) in there radiogroup 2 radiobuttons. when close dialog "save" button not save info in info class, want dialog open saved setting next time opened. tried accomplish radigroup methods: getcheckedradiobuttonid() , .check(int), solution doesn't work , don't know why.

my code:

settingsdialogfragment.java public class settingsdialogfragment extends dialogfragment{ private boolean ifmale; private int checkedradio; private radiogroup rg; public dialog oncreatedialog(bundle savedinstancestate) { // utilize builder class convenient dialog construction alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.settitle(r.string.settings); view view = layoutinflater.from(getactivity()).inflate(r.layout.settings_dialog, null); builder.setview(view); builder.setpositivebutton(r.string.save, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { data.ifmale = ifmale; checkedradio = rg.getcheckedradiobuttonid(); system.out.println("numer radio" +checkedradio); } }); builder.setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // user cancelled dialog } }); rg = (radiogroup) view.findviewbyid(r.id.radio_group); rg.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { @override public void oncheckedchanged(radiogroup group, int checkedid) { switch(checkedid) { case r.id.radio_female: //log.v("radiogroup", "female"); ifmale = false; break; case r.id.radio_male: log.v("radiogroup","male"); ifmale = true; break; } } }); rg.check(checkedradio); homecoming builder.create(); } } settings_dialog.xml <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <radiogroup android:id="@+id/radio_group" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <radiobutton android:id="@+id/radio_female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female"/> <radiobutton android:id="@+id/radio_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male"/> </radiogroup> </linearlayout>

you can utilize shared preference maintain track of checked radio button's information, , check next time when dialog opened.

code snippet:

sharedpreferences pref = getapplicationcontext().getsharedpreferences("radiopref", mode_private); editor editor = pref.edit(); //save info in shared preference on button click builder.setpositivebutton(r.string.save, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { data.ifmale = ifmale; checkedradio = rg.getcheckedradiobuttonid(); system.out.println("numer radio" +checkedradio); editor.putint("checkedid", checkedradio); } }); //get checkedid shared preference , check radion button. int checkedid = pref.getint("checkedid", -1); if(checkedid != -1) { //rg.check(checkedid); radiobutton rbutton =(radiobutton)findviewbyid(checkedid); rbutton.setchecked(true); }

hope helps.

java android android-dialogfragment android-radiogroup android-radiobutton

No comments:

Post a Comment