I create a Spinner
element inside of my LinearLayout
. I want to make values different from visible. I don't want to do that programmatically. I want to use arrays that below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="select">
<item>a</item>
<item>b</item>
<item>c</item>
</string-array>
<integer-array name="selectValues">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
</resources>
Simply. If a
selected item, I want to get 1
as integer. What's the way?
<Spinner
android:id="@+id/sSelect"
android:layout_width="179dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:entries="@array/select"
android:entryValues="@array/selectValues" />
When I use above with below.
public void onItemSelected(AdapterView<?> item, View arg1, int sort,
long arg3) {
// TODO Auto-generated method stub
String selectedItem = item.getItemAtPosition(sort).toString();
}
I just can get data as string and not values. I can get values that visible.
Keep the selected values as a TypedArray
and access those in onItemSelected()
method.
// Keep the selected values as TypedArray
Resources res = getResources();
final TypedArray selectedValues = res
.obtainTypedArray(R.array.selectValues);
Spinner spinner = ((Spinner) findViewById(R.id.sSelect));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//Get the selected value
int selectedValue = selectedValues.getInt(position, -1);
Log.d("demo", "selectedValues = " + selectedValue);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With