从列表preference获取整数或索引值 [英] Getting integer or index values from a list preference

查看:206
本文介绍了从列表preference获取整数或索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的共享preference创建列表,并当上preferenceChanged()方法被调用我想提取的项目在列表或索引在某些情况下一个整数值。我试图创建XML数据如下:

I'm creating lists in a shared preference and when the onPreferenceChanged() method is called I want to extract the index of the item in the list or an integer value in some cases. I am trying to build the xml data as follows:

在阵列中的:

<string-array name="BackgroundChoices">
  <item>Dark Background</item>
  <item>Light Background</item> 
</string-array>
<array name="BackgroundValues">
  <item>1</item>
  <item>0</item> 
</array>
<string-array name="SpeedChoices">
  <item>Slow</item>
  <item>Medium</item>
  <item>Fast</item>
</string-array>    
<array name="SpeedValues">
  <item>1</item>
  <item>4</item>
  <item>16</item>
</array>

在preferences xml文件:

in the preferences xml file:

<PreferenceScreen android:key="Settings"  
   xmlns:android="http://schemas.android.com/apk/res/android"  
   android:title="Settings">

<ListPreference
        android:key="keyBackground"
        android:entries="@array/BackgroundChoices"
        android:summary="Select a light or dark background." 
        android:title="Light or Dark Background"  
        android:positiveButtonText="Okay"   
        android:entryValues="@array/BackgroundValues"/>
<ListPreference 
        android:key="keySpeed" 
        android:entries="@array/SpeedChoices" 
        android:summary="Select animation speed."
        android:title="Speed" android:entryValues="@array/SpeedValues"/>
</PreferenceScreen>

所以我的XML不起作用。我知道如何使用一个字符串数组,而不是一个数组的值来做到这一点。我可以拉出来的字符串值并从中获得什么,我想从那个,但我宁愿(如果可能的话)能够有名单,其中值分别为整数,布尔值或枚举。什么是习惯性的方式来做到这一点?

So my xml does not work. I know how to do this using a string-array rather than an array for the values. And I could pull out the value strings and derive what I want from that but I would rather (if possible) be able to have lists where the values were ints, booleans, or enums. What is the customary way to do this?

在此先感谢,

周杰伦

推荐答案

把preferences作为字符串和使用的Integer.parseInt()。我认为,其实你是指在限制发送错误报告,但我找不到链接。从以往的经验,我可以告诉你,只使用字符串并保存你自己太多的无奈。

Put the preferences in as String and use Integer.parseInt(). I think there is actually a bug report on the limitation you are referring to but I can't find the link. From experience I can tell you to just use Strings and save your self a lot of frustration.

请注意,以其他SO用户,如果你能证明我错了,我欢迎它。

Note to other SO users, if you can prove me wrong, I welcome it.

这篇关于从列表preference获取整数或索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆