java - ListPreferences; Getting an option from a string-array -
i learning basics of android development through udacity. class asking make setting on app user chose whether temperatures should done in fahrenheit or in celsius. i've created array in xml user choose preference, don't know how pass info down java code. have provided few places problem might going wrong. of right now, code displays , works fine until user changes
pref_general.xml
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <edittextpreference android:key="location" android:title="location" android:defaultvalue="85383" android:inputtype="text" android:singleline="true"/> <listpreference android:title="@string/pref_units_label" android:key="@string/pref_units_key" android:defaultvalue="@string/pref_units_metric" android:entryvalues="@array/pref_units_values" android:entries="@array/pref_units_options"/> </preferencescreen>
arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="pref_units_values"> <item>"@st</item> <item>fahrenheit</item> <item>celsius</item> </string-array> <string-array name="pref_units_options"> </string-array> </resources>
strings.xml (part of it)
<resources> ... <string name="pref_units_label">temperature style</string> <string name="pref_units_key">"@array/pref_units_values"</string> <string name="pref_units_metric">celsius</string> <string name="pref_units_values">values</string> <string name="pref_units_imperial">fahrenheit</string> </resources>
forecastfragment.java (method uses listpreferences)
private string formathighlows(double high, double low) { sharedpreferences shared = preferencemanager.getdefaultsharedpreferences(getactivity()); string unittype = shared.getstring(getstring(r.string.pref_units_key), getstring(r.string.pref_units_metric)); if (unittype.equals(getstring(r.string.pref_units_imperial))) { high = (high * 1.8) + 32; low = (low * 1.8) + 32; } else if (!unittype.equals(getstring(r.string.pref_units_metric))) { log.d(log_tag, "unit type not found: " + unittype); } // presentation, assume user doesn't care tenths of degree. long roundedhigh = math.round(high); long roundedlow = math.round(low); string highlowstr = roundedhigh + "/" + roundedlow; return highlowstr; }
i feel issue might lie in xml files. have had experience before java, know next nothing xml. advice can offered appreciated. thank you.
can please more specific? you're trying make user choose between unities, believe first part define in way want display option in screen (for instance, radio button, listview, capture content in edittext), next add listener in layout item, when event happens, able capture value. in of these parts having problems?
Comments
Post a Comment