I have to develop an android view such that i have 2 spinner controls in it, one for state and the second for cities.
My question is, how can I populate the city spi
You have to do the folloowing step
create data source of cities (either database or in hashmap("state","city")).
set setOnItemSelectedListener(listener) to spinner containing states.
onItemSelected() method of the above listner fetch the data from datasouce created in the first step and attach it to city spinner.
thats it.
Here is the correct way dear....
I had wrote all the needed states and cities in string_arays....
like
<string-array name="State_array">
<item >s1</item>
<item >s2</item>
<item >s3</item>
<item >s4</item>
</string-array>
//then cities array for each states, like
<string-array name="State1Cities_array">
<item >c11</item>
<item >c12</item>
<item >c15</item>
<item >c13</item>
</string-array>
<string-array name="State2Cities_array">
<item >c1</item>
<item >c2</item>
<item >c5</item>
<item >c3</item>
</string-array>
// and so on for all the states
then in main xml added 2 spinners for both. i belive all of u can do i simple, na?
then i have my main.xml as...
spinner_states_activity = (Spinner)findViewById(R.id.spinner_states_main);
spinner_states_activity.setOnItemSelectedListener(this);
adapter = ArrayAdapter.createFromResource(
this, R.array.state_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.myspinner);
// my layout for spinners u can use urs or defalut. k?
spinner_states_activity.setAdapter(adapter);
spinner_cities_activity = (Spinner)findViewById(R.id.spinner_cities_main);
spinner_cities_activity.setOnItemSelectedListener(this);
//and in function onItemSelected
int pos,pos2;
pos = spinner_states_activity.getSelectedItemPosition();
int iden=parent.getId();
if(iden==R.id.spinner_states_main)
{
pos2 = 0;
switch (pos)
{
case 0: unit_adapter= ArrayAdapter.createFromResource(
this, R.array.States1Cities_array, android.R.layout.simple_spinner_item);
break;
case 1: unit_adapter= ArrayAdapter.createFromResource(
this, R.array.States3Cities_array, android.R.layout.simple_spinner_item);
break;
// all the StatesxCities entires....
default:
break;
}
spinner_cities_activity.setAdapter(unit_adapter);
unit_adapter.setDropDownViewResource(R.layout.myspinner);
}
Just check a look and do it by yourself... Hope this will helps u a little...
k dear friends.. Sujith
Example:
Spinner city=(Spinner)findViewById(R.id.citySpinner);
Spinner state=(Spinner)findViewById(R.id.stateSpinner);
final ArrayList<String> state_options=new ArrayList<String>();
final ArrayList<String> city_options=new ArrayList<String>();
state_options.add("state_1");
state_options.add("state_2");
state_options.add("state_3");
// Here you can also get a cursor and add Strings as options to state_options instead of what i have done
city_options.add("city_1_state_1");
city_options.add("city_2_state_1");
city_options.add("city_3_state_1");
// Here you can also get a cursor and add Strings as options to city_options instead of what i have done
ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,city_options);
city.setAdapter(cityAdapter);
ArrayAdapter<String> stateAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,state_options);
state.setAdapter(stateAdapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
String stateName=state_options.get(position).toString();
resetCity(stateName);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Now,
public void resetCity(String stateName)
{
city_options.removeAll(city_options);//i haven't checked this.
if(stateName.eqauls("state_1"))
{
city_option.add("city_1_state_1");
city_options.add("city_2_state_1");
city_options.add("city_3_state_1");
//you can also get a cursor and add Strings as options to city_options instead of what i have done
}
else if(stateName.eqauls("state_2"))
{
city_option.add("city_1_state_2");
city_options.add("city_2_state_2");
city_options.add("city_3_state_2");
// you can also get a cursor and add Strings as options to city_options instead of what i have done
}
else
{
city_option.add("city_1_state_3");
city_options.add("city_2_state_3");
city_options.add("city_3_state_3");
//you can also get a cursor and add Strings as options to city_options instead of what i have done
}
ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,city_options);
city.setAdapter(cityAdapter);
}
This is the simplest example.You can set your city_options
and state_options
from your database.and then you can use it for populating accoring spinners.
public class ConverterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner citySpinner = (Spinner)findViewById(R.id.spinner_city);
final ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[]); // starts empty
city_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_city.setAdapter(city_adapter);
Spinner categorySpinner = (Spinner)findViewById(R.id.spinner_state);
final ArrayAdapter<String> categoryAdapter = ArrayAdapter.createFromResource(
this, R.array.category_array, android.R.layout.simple_spinner_item);
categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setAdapter(category_adapter);
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedState = parent.getItemAtPosition(position);
cityAdapter.clear(); // remove previous entries
// look up your cities for the state selectedState, using your own method
cityAdapter.add(the cities you want); // do that in a loop for all your cities
cityAdapter.notifyDataSetChanged();
};
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
});
}
}
NB: coded in the StackOverflow editor, there might be some syntax errors :)
To lookup the cities for each state, use any method you want: you can define them in multiple string-array resources, or get them from a database, etc.