我在同一个活动中创建了2个微调器,
例如
每当选择国家时,它会显示一些国家,如日本,中国等......
如果我选择中国在另一个像国家那样仅限于中国各州和城市,或者如果我在另一个旋转器中选择日本,它只显示日本国家
答案 0 :(得分:0)
您可以通过
获取当前选中的项目String currentSelectedItem = mySpinner.getSelectedItem().toString();
OR
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
我在我的项目中做了同样的事情:
如果您正在使用从JSON获取Country数组而不是按照以下步骤操作:
注意:确保您还可以 country_id 和 state_id 通过 country id to state request参数为你想要的
第1步:首先,您应该将所有国家/地区填入数组并设置国家/地区适配器
第2步:比spinnerCountry.setAdapter()
onCreate()
你明白了
第3步:在spinnerCountry.setOnItemSelectedListener()
之后使用所选微调器项的位置获取country_id,将该country_id传递给状态请求参数以获取状态响应
第4步:您获得状态响应并将adater spinnerState.setAdapter()
设置为响应状态数组
答案 2 :(得分:0)
我最终在同一活动中添加了muliple微调器的代码。
公共类MainActivity扩展AppCompatActivity实现AdapterView.OnItemSelectedListener {
Spinner Cou, Sta, Cit;
Button button;
String name[]={"India","SriLanka"};
String flag1="India";
String flag2="SriLanka";
String flag3="Western Province";
String flag4="Central Province";
String flag5="Southern Province";
String name1[]={"Tamilnadu","kerala","karnataka"};
String name2[]={"Western","Central","Southern"};
String name3[]={"Colombo","Gampaha","Kalutara"};
String name4[]={"Kandy","Nuwara-Eliya","Matale"};
String name5[]={"Galle","Matara","Hambantota"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cou = (Spinner) findViewById(R.id.spinner);
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name);
Cou.setAdapter( arrayAdapter);
Cou.setOnItemSelectedListener(this);
Sta = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter aadapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name1);
Sta.setAdapter(aadapter);
Sta.setOnItemSelectedListener(this);
Cit = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter barrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name2);
Cit.setAdapter(barrayAdapter);
Cit.setOnItemSelectedListener(this);
Cit = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter carrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name3);
Cit.setAdapter(carrayAdapter);
Cit.setOnItemSelectedListener(this);
Cit = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter darrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name4);
Cit.setAdapter(darrayAdapter);
Cit.setOnItemSelectedListener(this);
Cit = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter earrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name5);
Cit.setAdapter(earrayAdapter);
Cit.setOnItemSelectedListener(this);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("data", String.valueOf(Cou.getSelectedItem()));
intent.putExtra("datam", String.valueOf(Sta.getSelectedItem()));
intent.putExtra("datama", String.valueOf(Cit.getSelectedItem()));
startActivity(intent);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
/*Toast.makeText(getApplicationContext(), "Welcome" + item, Toast.LENGTH_SHORT).show();*/
if(item=="India"){
Sta = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter aadapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name1);
Sta.setAdapter(aadapter);
Sta.setOnItemSelectedListener(this);
} else if(item=="SriLanka"){
Sta = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter aadapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name2);
Sta.setAdapter(aadapter);
Sta.setOnItemSelectedListener(this);
} else if(item=="Western"){
Cit = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter carrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name3);
Cit.setAdapter(carrayAdapter);
Cit.setOnItemSelectedListener(this);
} else if(item=="Central"){
Cit = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter darrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name4);
Cit.setAdapter(darrayAdapter);
Cit.setOnItemSelectedListener(this);
}else if(item=="Southern"){
Cit = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter earrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name5);
Cit.setAdapter(earrayAdapter);
Cit.setOnItemSelectedListener(this);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}