Associate values to spinner

166 views Asked by At

I want to implement in my application, certain mathematical calculation, but, it has a variable that covers age group and data, I will show in practice: 'Adequação da CB = CB obtida/CB percentile 50 * 100' . This is the formula, where the variable "CB percentile 50" has 28 different forms of being expressed, for example, from 1 to 2 years old it has the value of 16, from 2 to 3 has the value of 16.3, from 3 To 4 has the value of 16.8 and so on... How do I make the spinner check the age data and return this value to be placed in the mathematical expression?

My strings file

<string name="fxetaria">Selecione a Faixa Etária</string>
    <string-array name="faixaet">
        <item>1 a 1.9</item>
        <item>2 a 2.9</item>
        <item>3 a 3.9</item>
        <item>4 a 4.9</item>
        <item>5 a 5.9</item>
        <item>6 a 6.9</item>
        <item>7 a 7.9</item>
        <item>8 a 8.9</item>
        <item>9 a 9.9</item>
        <item>10 a 10.9</item>
        <item>11 a 11.9</item>
        <item>12 a 12.9</item>
        <item>13 a 13.9</item>
        <item>14 a 14.9</item>
        <item>15 a 15.9</item>
        <item>16 a 16.9</item>
        <item>17 a 17.9</item>
        <item>18 a 24.9</item>
        <item>25 a 29.9</item>
        <item>30 a 34.9</item>
        <item>35 a 39.9</item>
        <item>40 a 44.9</item>
        <item>45 a 49.9</item>
        <item>50 a 54.9</item>
        <item>55 a 59.9</item>
        <item>60 a 64.9</item>
        <item>65 a 69.9</item>
        <item>70 a 74.9</item>
    </string-array>

And my activity:

public class adeqcbh extends AppCompatActivity {

    Spinner spinner;
    ArrayAdapter<CharSequence> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adeqcbh);

        spinner = (Spinner)findViewById(R.id.spinner);
        adapter = ArrayAdapter.createFromResource(this, R.array.faixaet, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
1

There are 1 answers

4
opt05 On

I'm thinking the best way would be to have another array with the values (the same amount as your first array).

<string-array name="faixaet_values">
  <item>16</item>
  <item>16.3</item>
  ...
<string-array>

Then, in your onItemSelected method, just get the value from the new string array, like so;

String valueString = getResources().getStringArray(R.array.faixaet_values)[position - 1];
float value;
try {
  value = Float.valueOf(valueString);
} catch(NumberFormatException e) {
  Log.e(TAG, "This is not a number");
}