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

标签 java android android-spinner android-number-picker

我不确定这是否是正确的方法,我需要一些帮助来解决这个问题。我想从 NumberPicker 中选择一个数字。选择后,我希望 setOnValueChangedListener 能够捕获该值并运行 for 循环并将结果值存储在数组列表中。接下来,我希望循环值显示在我的 spinner 中。但我收到一条错误消息,指出“无法解析构造函数 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);
        }
    });

关于java - 根据选择的数字设置微调器值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417112/

相关文章:

java - 从java中的数组中找出n个缺失元素

java - Spring 中的 Cron 表达式

java - 无法弄清楚为什么 SQL 语句不起作用

android - Action Bar 中 Spinner 的主题在模拟器中正确显示,但在手机上显示错误

android - 如何执行选择微调项的任务

java - 从堆栈跟踪 : "MyClass.java:1" 中的第一行调用方法

用于调用 SOAP - Web 服务的 Java 类

android - 非法状态异常 :buildToolsVersion is not specified

android - 获取 View 文本(按钮)

android - AppCompat Spinner 的 TextInputLayout 错误对齐问题