我的微调器的条目是否可以取决于另一个微调器的位置?
我还没有接受过正式的编程培训(不过,我是大学新生,打算主修它),并且在只了解 Visual Basic 后尝试学习 Android,所以我需要很多帮助。这是我到目前为止的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.Books, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Here's what I need help with. I basically want it to say:
switch(position) {
case 0:
// Here I want it to set spinner2's entries to R.array.Five
case 1:
//Here I want to set spinner2's entries to R.array.Three
//etc...
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.Five, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//(something similar)
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
是否可以从另一个微调器的 onItemSelected 方法更改一个微调器的条目数组?我需要在哪里添加什么代码?试图在 spinner1 的 OnItemSelectedListener 中谈论 spinner2 或adapter2 时说它是一个错误的构造。这是有道理的,但我不知道如何解决这个问题。
I have had no formal training in programming at all yet (I'm a freshman in college, though, and going to major in it), and am trying to learn Android after only knowing Visual Basic, so I need a lot of help. Here's the code I have so far:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.Books, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Here's what I need help with. I basically want it to say:
switch(position) {
case 0:
// Here I want it to set spinner2's entries to R.array.Five
case 1:
//Here I want to set spinner2's entries to R.array.Three
//etc...
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.Five, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//(something similar)
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Is it possible to change the entries array of one spinner from another spinner's onItemSelected method? What code do I need to add where? Trying to say something about spinner2 or adapter2 in spinner1's OnItemSelectedListener says it's a misplaced construct. Which makes sense, but I don't know how to get around that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在 OnCreate 方法顶部定义替代 ArrayAdapter 并定义替代集即可。此外,将
spinner2
的定义移到OnItemSelectedListener
的定义之上,以便可以在内部类中访问它。微调器变量以及数组适配器必须最终确定才能在内部类中访问。只需在 swith-case 语句中调用
spinner2.setAdapter(....)
即可更改适配器。只要您只想处理微调器中的静态元素集,这就可以工作。
Just define the alternative ArrayAdapter(s) at top of OnCreate method with alternate sets defined. Further, move up the definition of
spinner2
above the definition ofOnItemSelectedListener
so it can be accessible inside the inner class. The spinner variable as well as the array adapters must be finalized to be accessible inside the inner class.Change the adapter by simply invoking
spinner2.setAdapter(....)
inside the swith-case statement.This will work as long as you only want to handle static sets of elements in your spinner.