Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

android - How-to init a ListPreference to one of its values

I'm trying to set a defaultValue to a ListPreference item.

Here is an sample of my preference.xml file:

<ListPreference android:key="notification_delay"
    android:title="@string/settings_push_delay"
    android:entries="@array/settings_push_delay_human_value"
    android:entryValues="@array/settings_push_delay_phone_value"
    android:defaultValue="????">
</ListPreference>

The two arrays:

<string-array name="settings_push_delay_human_value">
    <item>every 5 minutes</item>
    <item>every 10 minutes</item>
    <item>every 15 minutes</item>
</string-array>
<string-array
    name="settings_push_delay_phone_value">
    <item>300</item>
    <item>600</item>
    <item>900</item>
</string-array>

When i go into the preference activity, no item of the ListPreference is selected. I've tried to set an int value like 1 in the "android:defaultValue" fied to select "10 minutes" but it does not work.

<ListPreference android:key="notification_delay"
    android:title="@string/settings_push_delay"
    android:entries="@array/settings_push_delay_human_value"
    android:entryValues="@array/settings_push_delay_phone_value"
    android:defaultValue="1">
</ListPreference>

Any Idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to specify the value. So to get the first entry selected by default specify defaultValue="300" in your example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...