Reputation: 197
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.
Upvotes: 0
Views: 131
Reputation: 423
@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);
}
});
Upvotes: 0
Reputation: 915
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);
}
});
Upvotes: 1