简体   繁体   中英

Android ListPreference default value not working

This is my code

 <ListPreference 
         android:key="yearlength"
         android:title="Year Length"
         android:entries="@array/years"
         android:entryValues="@array/yearsvalues"
         android:summary="%s"
         android:defaultValue="365.256363004"/>  

      <string-array name="years">
        <item>Mean sidereal solar year(365.256363004)</item>
        <item>Mean tropical solar year(365.24219)</item>
        <item>Savana year(360)</item>
        <item>Thithi year(354)</item>
        <item>Nakshatra year(324)</item>
        <item>Normal solar year(365.2425)</item>
       </string-array>   

       <string-array name="yearsvalues">
        <item>365.256363004</item>
        <item>365.24219</item>
        <item>360</item>
        <item>354</item>
        <item>324</item>
        <item>365.2425</item>
       </string-array>

I am new to andorid.The default value is not showing. But when i set andorid:defaultValue="360" its working.plz help me

you should use string reference.

<string name="default>365.256363004</string>

and use

<ListPreference 
         android:key="yearlength"
         android:title="Year Length"
         android:entries="@array/years"
         android:entryValues="@array/yearsvalues"
         android:summary="%s"
         android:defaultValue="@string/default"/>

The default value inf is parsed as a float , converted to a String, and then stored as the Preference default value.

Add

android:defaultValue="inf"

More Details Beware of Preference Default Values in XML

<ListPreference 
         android:key="yearlength"
         android:title="Year Length"
         android:entries="@array/years"
         android:entryValues="@array/yearsvalues"
         android:summary="%s"
         android:defaultValue="365.256363004"/>

Dont fotget to Uninstall or clear application data and reinstall the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM