我在 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 代码中使用“获取选择选项值”
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最佳答案
试试这个方法,希望能帮助您解决问题。
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();
关于java - Spinner String 项目值获取(不是字符串名称)android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26098359/