spinner adding string array on item selection how can get item related value in android

前端 未结 1 1148
孤独总比滥情好
孤独总比滥情好 2021-02-08 02:37

i am developing one spinner this spinner i am string array

spinner = (Spinner)this.findViewById(R.id.mlg); 
final CharSequence[] itemArray =getResources().getTe         


        
相关标签:
1条回答
  • 2021-02-08 03:22

    I am not sure either Spinner allow that attribute value in XML String or not but your problem can be solved like this.

    Create two arrays in your array.xml file like:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="items">
            <item>Nokia 1200</item>
            <item>Nokia 1600</item>
            <item>Nokia 5130</item>
        </string-array>
        <string-array name="values">
            <item>1000</item>
            <item>2000</item>
            <item>5000</item>
        </string-array>
    </resources>
    

    Now load first array in your adapter and store the second one in other Array to hold values of items as:

    String items [] = getResources().getStringArray(R.array.items);
    String values [] =  getResources().getStringArray(R.array.values);
    

    And you can simply get the respective item name and value in your onItemSelected() method like this:

    String item = parent.getItemAtPosition(pos).toString();
    String value = values [pos];
    
    0 讨论(0)
提交回复
热议问题