Spinner将ArrayList <string>设置为值

时间:2017-10-22 13:06:45

标签: java android arraylist

我正在处理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()];

1 个答案:

答案 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) {

        }
    });