如何按位置由值设置微调选定项目,不 [英] How to set selected item of Spinner by value, not by position

查看:134
本文介绍了如何按位置由值设置微调选定项目,不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <项目值=245>几内亚比绍[245]< /项目>
<项目值=592>圭亚那[592] LT; /项目>
<项目值=509>海地[509] LT; /项目>
<项目值=504>洪都拉斯[504] LT; /项目>
<项目价值=91>印度[91]< /项目>
<项目价值=62>印度尼西亚[62]< /项目>


  

我用这


  country.setOnItemSelectedListener(新OnItemSelectedListener(){    公共无效onItemSelected(适配器视图<>母公司,观景,INT位置,长的id){        // TODO自动生成方法存根
        // selectioncountry = parent.getItemAtPosition(位置)的ToString();
        selectionarea = parent.getItemIdAtPosition(位置);
    }    @覆盖
    公共无效onNothingSelected(适配器视图<>为arg0){        // TODO自动生成方法存根
    }
});


  

这code给印度thie值是4,但我需要91我该怎么办
  这???



解决方案

您必须采取对选定的国家相应值多了一个字符串数组:

 <字符串数组名=国>
   <项目>印度和LT; /项目>
< /字符串数组><字符串数组名=country_value>
  <项目> 91 LT; /项目>
< /字符串数组>

初​​始化值数组:

 的String [] = countryValue getResources()getStringArray(R.array.country_value)。

从价值阵列获取微调所选项目的相应值:

  selectionarea = countryValue [位置]

检查示例:<一href=\"http://stackoverflow.com/questions/27057400/accessing-item-name-in-string-array-android/27057930#27057930\">accessing项目名称字符串数组(Android版)

<item value="245">Guinea-Bissau [245]</item >
<item value="592">Guyana [592]</item >
<item value="509">Haiti  [509]</item >
<item value="504">Honduras [504]</item >
<item value="91">India [91]</item >
<item value="62">Indonesia [62]</item >

I am using this

country.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        // TODO Auto-generated method stub
        //selectioncountry = parent.getItemAtPosition(position).toString();
        selectionarea = parent.getItemIdAtPosition(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

        // TODO Auto-generated method stub          
    }
});

this code give thie value of india is 4 but i need 91 how can i do this ???

解决方案

You have to take one more string array for selected country respective value :

<string-array name="country">
   <item>India</item>
</string-array>

<string-array name="country_value">
  <item>91</item>
</string-array>

Initialize value array :

String[] countryValue = getResources().getStringArray(R.array.country_value);

Get spinner selected item respective value from value array:

selectionarea = countryValue[position];

Check Example : accessing item name in string-array (Android)

这篇关于如何按位置由值设置微调选定项目,不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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