Tane Piper wrote:
> Hi there,
> 
> I'm have written a preference activity for my application, where I
> want to allow the user to currently set a timeout value for GPS.
> 
> In my XML I have this in array.xml:
> 
> <?xml version="1.0" encoding="utf-8"?>
> <resources>
>     <string-array name="list_gps_timeout">
>       <item value="20000">20 Seconds</item>
>       <item value="30000">30 Seconds</item>
>       <item value="60000">60 Seconds</item>
>     </string-array>
> </resources>
> 
> And in the layout file, I have this:
> 
> <?xml version="1.0" encoding="utf-8"?>
> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/
> android">
> 
> <ListPreference
>       android:entryValues="@array/list_gps_timeout"
>       android:entries="@array/list_gps_timeout"
>       android:title="GPS Timeout"
>       android:summary="The time that the GPS stops searching and falls back
> to the last know position."
>       android:key="gps_timeout"
>       android:defaultValue="20000">
> </ListPreference>
> </PreferenceScreen>
> 
> After setting the value, I then do this within my activity:
> 
> app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
> GPS_TIMEOUT = app_preferences.getInt("gps_timeout",
> DEFAULT_GPS_TIMEOUT);
> 
> However, what is returned is "20 Seconds" rather than the value 20000
> as an integer.  Can anyone see where I am going wrong?

You need two arrays, one with display values and one with the actual
numerical values you want.

For example:

<string-array name="foo_periods">
        <item>5 seconds</item>
        <item>30 seconds</item>
        <item>1 minute</item>
        <item>5 minutes</item>
        <item>10 minutes</item>
        <item>15 minutes</item>
        <item>30 minutes</item>
        <item>1 hour</item>
        <item>4 hours</item>
        <item>1 day</item>
</string-array>
<string-array name="foo_periods_secs">
        <item>5</item>
        <item>30</item>
        <item>60</item>
        <item>300</item>
        <item>600</item>
        <item>900</item>
        <item>1800</item>
        <item>3600</item>
        <item>14400</item>
        <item>86400</item>
</string-array>

And in your preferences XML:

<ListPreference
        android:key="foo"
        android:title="The Foo"
        android:summary="Click to select the foo time"
        android:entries="@array/foo_periods"
        android:entryValues="@array/foo_periods_secs"
        android:dialogTitle="Hi, Mom!" />

Note the android:entryValues attribute in the ListPreference.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
_The Busy Coder's Guide to Android Development_ Version 2.0 Available!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to