want a simple code for the getting the state name and city name depend on the selection

╄→尐↘猪︶ㄣ 提交于 2019-12-11 15:11:55

问题


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);
 }
});

.... }



来源:https://stackoverflow.com/questions/7314931/want-a-simple-code-for-the-getting-the-state-name-and-city-name-depend-on-the-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!