我在strings.xml中使用
<string-array name="country_arrays">
<item value="0">ValueName0</item>
<item value="1">ValueName1</item>
<item value="2">ValueName2</item>
<item value="3">ValueName3</item>
<item value="4">ValueName4</item>
</string-array>
我在Java Code For Get Select Option Value中使用
将String.valueOf(option.getSelectedItem())
Toast.makeText(AvpMain.this,
"Loading Wait : " + "\n Search : "+ sbox1 +
"\n("+ String.valueOf(option.getSelectedItem())+") Result",
Toast.LENGTH_SHORT).show();
如果用户选择ValueName3,则结果显示ValueName3 但我希望显示价值不是值得
例如。如果用户选择ValueName2,则结果显示:2
答案 0 :(得分:2)
请尝试这种方式,希望这有助于您解决问题。
<强>的strings.xml 强>
<array name="country_arrays">
<item>ValueName0</item>
<item>ValueName1</item>
<item>ValueName2</item>
<item>ValueName3</item>
<item>ValueName4</item>
</array>
<array name="country_value_arrays">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</array>
private ArrayList<String> countryList;
private ArrayList<String> countryValueList;
countryList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.country_arrays)));
countryValueList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.country_value_arrays)));
option.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(AvpMain.this,"Loading Wait : " + "\n Search : "+ sbox1 + "\n("+countryValueList.get(position)+") Result",Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
或强>
Toast.makeText(AvpMain.this,"Loading Wait : " + "\n Search : "+ sbox1 + "\n("+option.getSelectedItem().toString().charAt(option.getSelectedItem().toString().length())+") Result",Toast.LENGTH_SHORT).show();
答案 1 :(得分:0)
您必须在Item Selected Listener上实现微调器。
Try this
Spinner spinner;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(AvpMain.this,
"Loading Wait : " + "\n Search : " +
"\n("+positoin") Result",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
答案 2 :(得分:0)
您需要创建2个数组:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries">
<item>Portugal</item>
<item>France</item>
<item>Spain</item>
</string-array>
<string-array name="countries_codes">
<item>340</item>
<item>250</item>
<item>134</item>
</string-array>
</resources>
现在在适配器中加载第一个数组,并将第二个数组存储在其他数组中,以保存项目的值为:
String countries [] = getResources().getStringArray(R.array.counties);
String countries_codes [] = getResources().getStringArray(R.array.countries_codes);
您只需在onItemSelected()方法中获取相应的项目名称和值,如下所示:
String country = parent.getItemAtPosition(pos).toString();
String value = countries_codes [pos];