Android Prefererences -
i'm coming think problem preferences not being done correctly why cannot access tem. here preferences:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" > <preferencecategory android:title="@string/pref_user_profile" android:textsize="20px" android:layout="@layout/pref_layout"> <switchpreference android:title="@+string/pref_frequency" android:summary="@+string/pref_frequency_summary" android:key="frequency" android:defaultvalue="true" android:layout="@layout/pref_layout"/> <switchpreference android:title="@+string/pref_time" android:summary="@+string/pref_time_summary" android:key="time" android:defaultvalue="true" android:layout="@layout/pref_layout"/> <switchpreference android:title="@+string/pref_symptothermal" android:summary="@+string/pref_symptothermal_summary" android:key="symptothermal" android:defaultvalue="true" android:layout="@layout/pref_layout"/> <switchpreference android:title="@+string/pref_cervical_mucus" android:summary="@+string/pref_cervical_mucus_summary" android:key="cervical_mucus" android:defaultvalue="true" android:layout="@layout/pref_layout"/> <switchpreference android:title="@+string/pref_mucus_stamps" android:summary="@+string/pref_mucus_stamps_summary" android:key="mucus_stamps" android:defaultvalue="true" android:layout="@layout/pref_layout"/> <switchpreference android:title="@+string/pref_fertile_infertile" android:summary="@+string/pref_fertile_infertile_summary" android:key="fertile_infertil" android:defaultvalue="true" android:layout="@layout/pref_layout"/> </preferencecategory> </preferencescreen>
java:
package com.projectcaruso.naturalfamilyplaning; import com.projectcaruso.naturalfamilyplanning.r; import android.os.bundle; import android.preference.preferenceactivity; public class usersettingactivity extends preferenceactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.settings); } }
here call settings menu:
@override public boolean onoptionsitemselected(menuitem item) { final int result_settings = 1; switch (item.getitemid()) { case r.id.projectcaruso: util.gotogithub(this); return true; case r.id.about: new alertdialog.builder(this) .settitle(r.string.about) .setmessage(html.fromhtml(getstring(r.string.about_msg))) .show(); break; case r.id.licenses: new alertdialog.builder(this) .settitle(r.string.licenses) .setmessage(html.fromhtml(getstring(r.string.license_detail))) .show(); break; case r.id.contact: break; case r.id.settings: intent j = new intent(this, usersettingactivity.class); startactivityforresult(j, result_settings); break; } return super.onoptionsitemselected(item); } @override public boolean oncreateoptionsmenu(menu menu) { getsupportmenuinflater().inflate(r.menu.settings, menu); return true; } }
so appreciated! i'm trying access these preferences cannot. seems saving them fine. able test , run code, change pref's , saves state. when try access them cannot... here's code used try , access them:
edit: i've changed call following , no matter setting still "hello toast 2!" @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sharedpreferences preferences = this.getactivity().getsharedpreferences("usersettingactivity",0); // inflate layout fragment
boolean symptothermal = preferences.getboolean("symptothermal", true); if (!symptothermal) { context context = getactivity().getapplicationcontext(); toast.maketext(context, "hello toast 1!", toast.length_long).show(); } else { context context = getactivity().getapplicationcontext(); toast.maketext(context, "hello toast 2!", toast.length_long).show(); } if (!symptothermal) { textview temp = (textview) getview().findviewbyid(r.id.temp); temp.setvisibility(view.gone); } }
the problem how trying access them.
you use line:
sharedpreferences preferences = this.getactivity().getsharedpreferences("pref",0);
when access preferences stored through preferenceactivity should call default preferences.
sharedpreferences mpreferences = preferencemanager.getdefaultsharedpreferences(this);
that it.
edit
if making call getdefaultsharedpreferences()
fragment, need change value pass parameter. this
in above example context, call fragment, following:
sharedpreferences mpreferences = preferencemanager.getdefaultsharedpreferences(getactivity());
Comments
Post a Comment