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

﹥>﹥吖頭↗ 提交于 2019-12-03 06:03:01

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];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!