6

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Yusuf Ali
  • 173
  • 1
  • 10

1 Answers1

14

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

    }
});
imranhasanhira
  • 357
  • 2
  • 7