如何根据值而不是位置设置选定的微调控制项

时间:2023-01-19 09:03:21
<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 ???

这个代码给出的印度的thie值是4,但是我需要91我怎么做???

5 个解决方案

#1


1  

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:

从值数组中获取spinner选择项的值:

selectionarea = countryValue[position];

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

检查示例:在string-array (Android)中访问项目名称

#2


8  

private void selectSpinnerValue(Spinner spinner, String myString)
     {
         int index = 0;
         for(int i = 0; i < spinner.getCount(); i++){
             if(spinner.getItemAtPosition(i).toString().equals(myString)){
                 spinner.setSelection(i);
                 break;
             }
         }
     }

#3


3  

try this. it will work fine.

试试这个。它会正常工作。

int position = adapter.getPosition(value);
spinner.setSelection(position);

#4


0  

for(int i = 0; i < country.getCount(); i++){
    if(country.getItemAtPosition(i).toString().equals("yourString")){
        country.setSelection(i);
        break;
    }
}

You could also solve it by this way, by specifiying the String you are searching for instead if you have access to it. Otherwise, you will have to follow Haresh Chhelana's approach.

你也可以用这种方法来解决它,如果你有权限的话,你可以指定你要搜索的字符串。否则,你将不得不遵循Haresh Chhelana的方法。

#5


0  

You can always override getItemId(int position) to return the value instead of the position id which is the default

您总是可以覆盖getItemId(int position)以返回值,而不是默认的位置id

#1


1  

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:

从值数组中获取spinner选择项的值:

selectionarea = countryValue[position];

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

检查示例:在string-array (Android)中访问项目名称

#2


8  

private void selectSpinnerValue(Spinner spinner, String myString)
     {
         int index = 0;
         for(int i = 0; i < spinner.getCount(); i++){
             if(spinner.getItemAtPosition(i).toString().equals(myString)){
                 spinner.setSelection(i);
                 break;
             }
         }
     }

#3


3  

try this. it will work fine.

试试这个。它会正常工作。

int position = adapter.getPosition(value);
spinner.setSelection(position);

#4


0  

for(int i = 0; i < country.getCount(); i++){
    if(country.getItemAtPosition(i).toString().equals("yourString")){
        country.setSelection(i);
        break;
    }
}

You could also solve it by this way, by specifiying the String you are searching for instead if you have access to it. Otherwise, you will have to follow Haresh Chhelana's approach.

你也可以用这种方法来解决它,如果你有权限的话,你可以指定你要搜索的字符串。否则,你将不得不遵循Haresh Chhelana的方法。

#5


0  

You can always override getItemId(int position) to return the value instead of the position id which is the default

您总是可以覆盖getItemId(int position)以返回值,而不是默认的位置id