ListPreferences; Getting an option from a string-array

123 views Asked by At

I am currently learning the basics of Android Development through Udacity. The class is now asking I make a setting on the app for the user to chose whether the temperatures should be done in Fahrenheit or in Celsius. I've created an array in xml for the user to choose their preference, but I don't know how to pass this info down to my java code. I have provided a few places where this problem might be going wrong. As of right now, the code displays and works fine up until the user changes the

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 that uses the 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);
            }
            // For presentation, assume the user doesn't care about tenths of a degree.
            long roundedHigh = Math.round(high);
            long roundedLow = Math.round(low);
            String highLowStr = roundedHigh + "/" + roundedLow;
            return highLowStr;
        }

I feel that the issue might lie in the xml files. I have had experience before with java, but I know next to nothing about xml. Any advice that can be offered is appreciated. Thank you.

1

There are 1 answers

3
E. Fernandes On

Can you please be more specific? You're trying to make the user choose between the unities, I believe that the first part for that would be to define in which way you want to display this option in the screen (for instance, radio button, a ListView, capture the content in a EditText), next you will add some Listener in this layout item, so when some event happens, you will be able to capture its value. In which of these parts are you having problems?