-->

want a simple code for the getting the state name

2019-08-16 17:49发布

问题:

I am new to both android and java. I am developing a simple app which contain country , state and city which are selected by spinner. Now consider while am selecting the country(India) then i need to get only states of india. And then while selecting any state(Andhra pradesh) then the cities of A.P should be shown in the next spinner. Can any one suggest me with some sample code.

thanks in advance

回答1:

you can add this logic(for two spinners) to your code:

 public void onCreate() {
 ....
Country[] mCountries = ... ;
final Spinner spinner1 = ...;
final Spinner spinner2 = ...;

spinner1.setAdapter(new ArrayAdapter(mCountries);
spinner1.setOnItemSelectedListener( new OnItemSelectedListener() {

void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Country country = (Country) parent.getAdapter().getItem(position);
    spinner2.setAdapter(new ArrayAdapter(country.getStates());
 }
 void onNothingSelected(AdapterView<?> parent) {
    spinner2.setAdapter(null);
 }
});

.... }