【发布时间】:2015-01-25 06:50:01
【问题描述】:
我有一个充满整数值的微调器,我正在尝试提取选定的值。我设法使用以下代码行:
Integer.decode(spinner.getSelectedItem().toString())
但这似乎需要一个简单的操作。
我正在使用定义如下的字符串数组创建微调器:
在strings.xml中
<string-array name="settings_cells_array">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
在活动的onCreate函数中:
Spinner spinner = (Spinner) findViewById(R.id.num_cells_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.settings_cells_array, R.layout.my_layout);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(R.layout.my_layout);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
在我看来,由于微调器是使用字符串数组创建的,.toString() 调用似乎过多,但我想这为由自定义对象或类似东西构建的适配器和微调器留出了灵活性。
无论如何,我的问题是:
有没有更好的方法从以这种方式定义的微调器中提取整数,或者,是否有更好的方法来设置我的微调器,使其与 ints 配合得更好?
【问题讨论】:
标签: java android spinner arrays