Spinner2 基于 Spinner 1 中的选择进行动态更新
使用此代码(改编自 此处)我收到以下错误:
构造函数ArrayAdapter(new AdapterView.OnItemSelectedListener(){}, int, String[]) 未定义
任何人都可以帮助纠正这个问题吗?谢谢
private void setDynamicSpinnerBE() {
beSpinner = (Spinner) findViewById(R.id.spBE);
final String[] itemsSpinner2;
lensSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Integer focalLength = (Integer) parent.getAdapter().getItem(
position);
itemsSpinner2 = getBellowsExtension(focalLength, maxBellows,
precision);
ArrayAdapter<CharSequence> beAdapter = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item,
itemsSpinner2);
beAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
beSpinner.setAdapter(beAdapter);
}
public void onNothingSelected(AdapterView<?> parent) {
beSpinner.setAdapter(null);
}
}
);
...
}
In using this code (adapted from here) I get the following error:
The constructor ArrayAdapter(new
AdapterView.OnItemSelectedListener(){},
int, String[]) is undefined
Can anyone help correct this? Thanks
private void setDynamicSpinnerBE() {
beSpinner = (Spinner) findViewById(R.id.spBE);
final String[] itemsSpinner2;
lensSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Integer focalLength = (Integer) parent.getAdapter().getItem(
position);
itemsSpinner2 = getBellowsExtension(focalLength, maxBellows,
precision);
ArrayAdapter<CharSequence> beAdapter = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item,
itemsSpinner2);
beAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
beSpinner.setAdapter(beAdapter);
}
public void onNothingSelected(AdapterView<?> parent) {
beSpinner.setAdapter(null);
}
}
);
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在新
ArrayAdapter
的构造函数中的以下行:this
指向您所在的OnItemSelectedListener
类的当前实例,而不是父类查看,这就是问题发生的地方,因为 ArrayAdapter 没有匹配的构造函数。您应该尝试使用MyParentView.this
(其中MyParentView
是您所在视图的名称)来传递适当的实例。In the constructor to your new
ArrayAdapter
on the following line:this
points to the current instance of theOnItemSelectedListener
class you are in and not the parent view, this is where the problem occurs becauseArrayAdapter
does not have a matching constructor. You should try usingMyParentView.this
(whereMyParentView
is the name of the view you are in) instead to pass the appropriate instance.是的,这里是:
Yeah, here it is: