简体   繁体   English

如何将微调器中的 Integer[] 转换为 int 或 Integer 以进行计算

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

I'm a new students in android studio.我是android studio的新生。 I need you help resolve issue about spinner in JAVA我需要你帮助解决有关 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".您需要将其用作“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.您可以通过 spinner.getSelectedIndex() 或类似方法获取索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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