我正在处理spinner
而我正在努力实现的目标是将Arraylist
设置为spinner
上所选项目的值,但我得到了什么是arrayList
是在spinner
下显示为选项的那个。
我就是这样做的:
final Spinner spinner = (Spinner) findViewById(R.id.numSpinner);
ArrayList<String> numArr = new ArrayList<String>();
numArr.add("09054802841");
numArr.add("09123456789");
ArrayAdapter<String> numAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, numArr);
spinner.setAdapter(numAdapter);
这就是我从spinner
获取值的方法,这就是我知道错误发生的地方,因为 Recipient_value_array
是我选择项目时得到的值在spinner
上,它应该是Arraylist
值。如何正确设置?
itemSpin = getResources().getStringArray(R.array.recipient_value_array)[spinner.getSelectedItemPosition()];
答案 0 :(得分:0)
要获得Spinner
的值,请添加setOnItemSelectedListener
像:
final Spinner spinner = (Spinner) findViewById(R.id.numSpinner);
final ArrayList<String> numArr = new ArrayList<String>();
numArr.add("09054802841");
numArr.add("09123456789");
ArrayAdapter<String> numAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, numArr);
spinner.setAdapter(numAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String value = numArr.get(i);
Log.i("MainActivity", "Response: " + value);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});