android - Using sharedPreferences with a material spinner to update activities -
i found amazing material spinner online , able modify code:
<fr.ganfra.materialspinner.materialspinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="fill_parent" app:ms_multiline="false" app:ms_basecolor="@color/off" app:ms_highlightcolor="@color/off" app:ms_hintcolor="@color/off" app:ms_arrowcolor="@color/off" app:ms_arrowsize="16dp" app:ms_alignlabels="false" app:ms_floatinglabelcolor="@color/off"/>
so used sharedpreferences store whatever value user chooses spinner when page called again, choice on spinner face. how add preferences in oncreate:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.settings); string[] items = {"daily", "weekly", "monthly"}; arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, items); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner = (materialspinner)findviewbyid(r.id.spinner); spinner.setadapter(adapter); spinner.seterror(null); spinpref=getsharedpreferences("frequency", mode_private); spinedit=spinpref.edit(); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { if (position == 1) { int freq1 = 1; spinedit.putint("freq", freq1); spinedit.apply(); } else if (position == 2) { int freq2 = 2; spinedit.putint("freq", freq2); spinedit.apply(); } else if (position == 3) { int freq3=3; spinedit.putint("freq", freq3); spinedit.apply(); } } @override public void onnothingselected(adapterview<?> parent) { int freq0 = 0; spinedit.putint("freq", freq0); spinedit.apply(); } }); finish(); }
then in onresume, this:
@override protected void onresume() { spinpref=getsharedpreferences("frequency",mode_private); int num=spinpref.getint("freq", 0); spinner.setselection(num); }
but spinner shows first value, how make work? don't think sharedprefs wrong, may onitemselected() , i'm not sure how else this? ps: tried replacing line each of positions as
spinedit.putint("freq", position);
instead of have there right now:
spinedit.putint("freq", freq);
but didn't work either. ideas or material willing bring light me please? appreciate much
what i'm proposing (in comment above):
public class myactivity extends activity { sharedpreferences spinpref; editor spinedit; //prevent typos private static final string pref_name = "frequency"; public static final string frequency = "freq"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.settings); //init prefs spinpref=getsharedpreferences(pref_name, 0); spinedit=spinpref.edit(); //init spinner ... //set listener ... } @override protected void onresume() { int num=spinpref.getint(frequency, 0); spinner.setselection(num); } }
Comments
Post a Comment