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
434 views
in Technique[技术] by (71.8m points)

android - Can I give an ID to an item in String-array?

My question is simple, yet I don't know the answer.

WHAT I WANT:

<string-array name="items_array">
        <item id="id1">Item1</item>
</string-array>

THIS IS WHAT I CURRENTLY HAVE:

<string-array name="items_array"> 
   <item>Item1</item>
</string-array>

What do I want to achieve with that is, that I want to offer in my Spinner normal names (e.g. Martin). Yet in the ID of that item, I want to have e.g. "martin93".

The ID is usefull to append it to my URL, from which I want to fetch some data. But I want the user to choice a "normal" name, instead of weird (for example FaceBook) url name.

IMPORTANT: The facebook example is just given for explanation purposes, I am trying to achieve something else, but very similar (with ID I want to fetch the real URL's name, that is not nice to read, if I'd put it directly into Spinner).

Any ideas?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Typically you would have

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>

    <string-array name="planets_array_values" translatable="false">
        <item>merc</item>
        <item>ven</item>
        <item>eart</item>
        <item>mars</item>
    </string-array>
</resources>

Then populate the spinner with planets_array, and

public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    String value = getResources().getStringArray(R.array.planets_array_values)[pos];
}

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

...