开发者

Use Java constants in arrays defined by XML

I have an array like this that defines the entry values for a ListPreference:

<string-array name="sortSelectionEntryValues">
<item>0</item>
<item>1</item> 
</string-array>

Now instead of using 0 and 1 in XML, I want to use Java constants like e.g. ORDER_ASC and ORDER_DESC, because later I will access the value of the selected ListPreference entry programmatically and I have to check the value in code, but comparing the value to a well named consta开发者_如何学Pythonnt makes code easier to read.

So what I want is something like that:

In Java:
class MyOrderClass
{
    public static final String ORDER_ASC="0";
    public static final String ORDER_DESC="1";
}

In the XML:
<string-array name="sortSelectionEntryValues">
<item>MyOrderClass.ORDER_ASC</item>
<item>MyOrderClass.ORDER_DESC</item> 
</string-array>

Is that somehow possible? Thanks for any hint!

(Please note, the ordering is just a dumb example, I just need to know how to integrate Java constants into the XML definition of an array)


That's not possible. That said, you will never have to compare "0" to anything. You will likely use if(MyOrderClass.ORDER_ASC.equals(<selected item from list>){..} so it shouldn't be an issue.


You cant do it the way that you suggest, but...

You could probably do it a different way and define the constant in XML in a values file and define both your static in java, and your array in XML in terms of the defined value. This way it is only defined once and the same value is used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜