我还没有接受过任何正式的编程培训(虽然我是大学新生,并且主修它),并且我只是在了解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的东西,说这是一个错位的构造。这是有道理的,但我不知道如何解决这个问题。
答案 0 :(得分:1)
只需在OnCreate方法的顶部定义备用ArrayAdapter,并定义备用集。此外,将spinner2
的定义提升到OnItemSelectedListener
的定义之上,以便可以在内部类中访问它。必须最终确定spinner变量以及数组适配器才能在内部类中访问。
只需在swith-case语句中调用spinner2.setAdapter(....)
即可更改适配器。
只要您只想在微调器中处理静态元素集,这将有效。