的keyValue如下問題:得到ListPreference
preferences.xml
:
<ListPreference
android:title="titel"
android:summary="summary"
android:key="remove_onclick"
android:entries="@array/removeOnclick"
android:entryValues="@array/removeOnclick_value"
android:defaultValue="3"/>
我的問題是,我怎樣才能得到的值(1
,2
,或3
)與一個if條件使用在我的MainActivity.java
SharedPreferences sharedConfig = PreferenceManager.getDefaultSharedPreferences(this);
String removeOnklick = sharedConfig.getString("remove_onclick", "3");
這是行不通的!
引起:java.lang.ClassCastException:java.lang.Boolean中不能 鑄造爲java.lang.String在 com.notification.app.SettingsActivity.onCreate(SettingsActivity.java:14)
SettingsActivity:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
編輯 strings.xml
<string-array name="removeOnclick">
<item>Always</item>
<item>Never</item>
<item>Always ask</item>
</string-array>
<string-array name="removeOnclick_value">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
而且我也有2個完美的工作布爾值。
boolean playSound = sharedConfig.getBoolean("sound_on_create", false);
boolean vibrate = sharedConfig.getBoolean("vibrate_on_create", false);
更新:
我不得不刪除緩存和內存...
是什麼內容'arrays.xml'? (如果這是一個錯誤的問題,你在哪裏存儲'@ array'的東西?) –
在strings.xml中作爲字符串數組 – Landei00