0

I am not sure if this is the right way, I need some help to solve this problem.I want to select a number from the NumberPicker. After selecting, I expect the setOnValueChangedListener to be able to capture the value and run a for loop and store the resulting values in an arraylist. Next, i want the loop values to be displayed in my spinner . But i am getting an error message saying "cannot resolve constructor ArrayAdapter(....)" and when i run the app, i get this error message :

"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);
    }
    });
}

Thank you.

topacoBoy
  • 197
  • 2
  • 4
  • 17

2 Answers2

1

change from this to YourActivity.this

new ArrayAdapter(this, ...)

to

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

check this code.

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);
        }
    });
myoungjin
  • 895
  • 1
  • 13
  • 27
0

@topacoboy on scroll you want to add number in spinner? try following code.

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);
        }
    });
Rahul Wadhai
  • 413
  • 1
  • 4
  • 9
  • Remove for loop. add direct before adding in array list check this number list.contain() if number is present then don't add. – Rahul Wadhai Apr 05 '16 at 05:28
  • I wanted it to loop the value selected by the user and display it so that is why i needed the for loop. Anyway i got i fixed. Thanks for the help man @RahulWadhai – topacoBoy Apr 05 '16 at 05:39