简体   繁体   中英

How to convert Integer[] in spinner to int or Integer for calculate

I'm a new students in android studio. I need you help resolve issue about spinner in JAVA

below is my code for create spinner and need to get value from spinner to calculate.

How to input code ?

    final Spinner spinner = findViewById(R.id.spn);
    final Integer[] period = new Integer[]{12, 24, 36, 48, 60, 72, 84};
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, period);
    spinner.setAdapter(adapter);

Code for calculate.

    int Result1;
    Result1 = (period / 12);

Screenshot Error

The period is an array. You need to use it as "period[yourIndex]/12".

int result1 = period[0]/12 (this results on 12/12 = 1).
int result2 = period[1]/12 (results 24/12 = 2).

You can get index by spinner.getSelectedIndex() or some like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM