我不确定这是否正确,我需要一些帮助来解决这个问题。我想从NumberPicker中选择一个数字。选择之后,我希望setOnValueChangedListener能够捕获值并运行for循环并将结果值存储在arraylist中。接下来,我希望循环值显示在我的微调器中。但我收到一条错误消息说"无法解析构造函数ArrayAdapter(....)"当我运行应用程序时,我收到此错误消息:
"错误:(79,58)错误:找不到适合ArrayAdapter(,int,List)的构造函数 构造函数ArrayAdapter.ArrayAdapter(Context,int,int,List)不适用 (实际和正式的参数列表长度不同) 构造函数ArrayAdapter.ArrayAdapter(Context,int,List)不适用 (实际参数不能通过方法调用转换转换为Context) 构造函数ArrayAdapter.ArrayAdapter(Context,int,int,Object [])不适用 (实际和正式的参数列表长度不同) 构造函数ArrayAdapter.ArrayAdapter(Context,int,Object [])不适用 (实际参数不能通过方法调用转换转换为Context) 构造函数ArrayAdapter.ArrayAdapter(Context,int,int)不适用 (实际参数不能通过方法调用转换转换为Context) 构造函数ArrayAdapter.ArrayAdapter(Context,int)不适用 (实际和正式的参数列表长度不同)"
@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);
}
});
}
谢谢。
答案 0 :(得分:1)
从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);
}
});
答案 1 :(得分:0)
@topacoboy滚动你想在微调器中添加数字?尝试下面的代码。
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);
}
});