How to set StringArray value on click on array item in android?

Tanvir Durlove :

I'm working with an android spinner. Here I have two arrays in my XML like below and I'm showing car array in mine spinner.

<string-array name="car">
    <item>128i Coupe</item>
    <item>M3 Coupe</item>
    <item>M5 Sedan</item>
</string-array>
<string-array name="value">
    <item>1</item>
    <item>0</item>
    <item>2</item>
</string-array>

Now I can call the any of them in my Java file like this

String[] BMW_Model = MainActivity.this.getResources().getStringArray(R.array.car);

I can get/print the value of this item on click by this way

String td = spinnerManufacture.getSelectedItem().toString();

Now what I need is when I click on an item of my car array I should display the value of my second value array according to the position. As an example.

If I click on M5 Sedan from my spinner item it should show 2 in a Toast message from my second array.

Any kind of suggestion will be highly appreciated.

Сергей Бувака :

Since you have completely different arrays, the solution to your problem could be to get the position of the pressed spinner element and get the value of the second array at this index.

int position = spinner.getSelectedItemPosition()

String[] valueArray = context.getResources().getStringArray(R.array.value);

Toast.makeText(context,  valueArray[position],Toast. LENGTH_SHORT).show(); 

If you need to display toast on click, then you need to set clickListener

spinner.setOnItemSelectedListener(new

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174938&siteId=1