根据选择的数字设置微调值?

Setting spinner value depending on number picked?

我不确定这是否正确,我需要一些帮助来解决这个问题 problem.I 想 select 来自 NumberPicker 的数字。在 selecting 之后,我希望 setOnValueChangedListener 能够捕获值并 运行 一个 for 循环并将结果值存储在数组列表中。接下来,我希望循环值显示在我的微调器中。但是我收到一条错误消息说 "cannot resolve constructor ArrayAdapter(....)",当我 运行 应用程序时,我收到此错误消息:

"Error:(79, 58) error: no suitable constructor found for ArrayAdapter(,int,List) constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable (actual and formal argument lists differ in length) constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable (actual argument cannot be converted to Context by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int,int,Object[]) is not applicable (actual and formal argument lists differ in length) constructor ArrayAdapter.ArrayAdapter(Context,int,Object[]) is not applicable (actual argument cannot be converted to Context by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable (actual argument cannot be converted to Context by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable (actual and formal argument lists differ in length)"

      @Override    
     protected void onCreate(Bundle savedInstanceState) {        

super.onCreate(savedInstanceState);
           setContentView(R.layout.new_item);
           final List<Integer> resultInteger = new ArrayList<Integer>(); // to store the integer value

   final Spinner hourSpinner = (Spinner) findViewById(R.id.hourSpinner);


    final NumberPicker HourPicker = (NumberPicker) findViewById(R.id.HourNumberPicker);
    HourPicker.setMinValue(9);
    HourPicker.setMaxValue(15);
    HourPicker.setWrapSelectorWheel(false);

    HourPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            Integer value = HourPicker.getValue();

            for (int index = 0; index == value; index++)
            {
                resultInteger.add(index);
            }

          ArrayAdapter<CharSequence> hourAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, resultInteger);
            hourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            hourSpinner.setAdapter(hourAdapter);
    }
    });
}

谢谢。

this 更改为 YourActivity.this

new ArrayAdapter(this, ...)

new ArrayAdapter<Integer>(YourActivity.this, ...)

检查此代码。

hourPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            Integer value = hourPicker.getValue();

            resultInteger.clear();
            for (int index = 0; index < value; index++) {
                resultInteger.add(index);
            }

            ArrayAdapter<Integer> hourAdapter = new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_spinner_item, resultInteger);
            hourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            hourSpinner.setAdapter(hourAdapter);
        }
    });

@topacoboy on scroll 你想在微调器中添加数字吗?试试下面的代码。

HourPicker = (NumberPicker) findViewById(R.id.numberPicker);
    HourPicker.setMinValue(9);
    HourPicker.setMaxValue(15);
    HourPicker.setWrapSelectorWheel(false);
     resultInteger = new ArrayList<Integer>();
    HourPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            Integer value = HourPicker.getValue();

           // for (int index = 0; index == value; index++) {
                resultInteger.add(value);
           //}

            ArrayAdapter<Integer> hourAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, resultInteger);
            hourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            hourSpinner.setAdapter(hourAdapter);
        }
    });