嘿,我想问一下如何将选定的值从 1 个微调器传递到下一个微调器。例如,如果:
Spinner 1 = "School" is selected
Spinner 2 = Shows sub items for the selected item "School"
或者
Spinner 1 = "Office" is selected
Spinner 2 = Shows sub items for the selected item "Office".
在第一个微调器 (Spinner 1) 的 onItemSelected 列表器中绑定第二个微调器 (Spinner 2) 中的数据
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// bind data in second spinner as per you select
// in this first spinner (Spinner 1)
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
看看这个示例代码,这肯定会帮助你
public class MainActivity extends Activity {
Spinner s1,s2;
Button btn;
String s;
TextView tv;
ArrayAdapter<String> adap1,adap2,adap3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1=(Spinner)findViewById(R.id.spinner1);
s2=(Spinner)findViewById(R.id.spinner2);
btn=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.tv1);
String[] v1=getResources().getStringArray(R.array.c1);
String[] v2=getResources().getStringArray(R.array.c2);
String[] v3=getResources().getStringArray(R.array.c3);
adap1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v1);
adap2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v2);
adap3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ItemSelectedListenerr implements OnItemSelectedListener {
String[] v=getResources().getStringArray(R.array.c1);
public String s;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
s=arg0.getItemAtPosition(arg2).toString();
if(arg2==0)
{
s2.setAdapter(adap1);
}
if(arg2==1)
{
s2.setAdapter(adap2);
}
if(arg2==2)
{
s2.setAdapter(adap3);
}
tv.setText(s);
Log.i("hahaha", "item selected is"+s);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
它会像魅力 100 % 一样工作。:D