[英]Android Spinner get selected item
I have created 2 spinner in an same activity,我在同一个活动中创建了 2 个微调器,
for example例如
Whenever, if Country is selected it will display some countries like Japan,China, etc...无论何时,如果选择了国家/地区,它将显示一些国家/地区,例如日本、中国等...
If i select China in another spinner like state which diaplays only china states and city or if i select japan in another spinner it display only japan states如果我在另一个微调器中选择中国,它只显示中国的州和城市,或者如果我在另一个微调器中选择日本,它只显示日本的州
You can get currently selected item by您可以通过以下方式获取当前选择的项目
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) {
}
});
I have done same thing in my project :我在我的项目中做了同样的事情:
If you use are getting Country array from JSON than Just follow the step as bellow :如果您使用的是从 JSON 获取 Country 数组,请按照以下步骤操作:
Note: Make sure you also get country_id and state_id to pass country id to state request parameter for same what you want
注意:确保您还获得country_id和state_id以将国家 ID 传递给您想要的相同请求参数
step 1: First you should fill all countries in to array and set country adapter步骤 1:首先,您应该将所有国家/地区填入数组并设置国家/地区适配器
step 2: than spinnerCountry.setAdapter()
on onCreate()
hop you understood第 2 步:比你理解的
onCreate()
跳跃上的spinnerCountry.setAdapter()
step 3: after than spinnerCountry.setOnItemSelectedListener()
you are getting the country_id using position of selected spinner item, pass that country_id to state request parameter for getting state response第 3 步:在
spinnerCountry.setOnItemSelectedListener()
您将使用所选微调项的位置获取 country_id,将该 country_id 传递给状态请求参数以获取状态响应
step 4: Thats all you get state response and set adater spinnerState.setAdapter()
with your response state array第 4 步:这就是您获得状态响应并使用响应状态数组设置适配器
spinnerState.setAdapter()
I've finaly added the code for muliple spinner in same activity.我最终在同一活动中添加了多重微调器的代码。
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {公共类 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) {
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.