Thamarai T
Thamarai T

Reputation: 262

Dynamic population of dropdown based on another dropdown in xamarin android

I am in need of getting state and corresponding cities from selected country, in Xamarin.Android (need not in Xamarin.Forms)

I populate the countries in Spinner control as shown below

Spinner spinnerMailingCountry;
int[] countryId = new int[] { 0, 58, 98, 105, 86 };
String[] countryName = { "Select", "England", "Germany", "India"};
ArrayAdapter<String> countryAdapter;
String countryIdByPosition;
int selectedPosition;

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            ...
spinnerMailingCountry = FindViewById<Spinner>(Resource.Id.spinnerMailingCountry);

SetupCountrySpinner();
}
void SetupCountrySpinner()
        {
            countryAdapter = new ArrayAdapter<string>(this, Resource.Layout.spinner_item, countryName);
            countryAdapter.SetDropDownViewResource(Resource.Layout.spinner_item);

            spinnerMailingCountry.Adapter = countryAdapter;
            spinnerMailingCountry.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CountrySpinner_ItemSelected);
        }

private void CountrySpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            var spinner = (Spinner)sender;
            selectedPosition = spinner.SelectedItemPosition;
            countryIdByPosition = countryId[selectedPosition].ToString();
        }

Now once a country is selected, I need to populate list of corresponding state in spinner. This flow is also for city too. How can I achieve this?

NOTE: State and city lists are getting from my database through API call.

Upvotes: 0

Views: 496

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

Now once a country is selected, I need to populate list of corresponding state in spinner. This flow is also for city too. How can I achieve this?

When CountrySpinner_ItemSelected be invoked, you can set ArrayAdapter for State.And when State be selected, you can set ArrayAdapter for City.

axml:

<Spinner
    android:id="@+id/spinnerCountry"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dialog" />

<Spinner
    android:id="@+id/spinnerState"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dialog" />

<Spinner
    android:id="@+id/spinnerCity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dialog" />

Activity.cs : Adding test data( *stateName / cityName *)

Spinner spinnerMailingCountry;
Spinner spinnerMailingState;
Spinner spinnerMailingCity;
int[] countryId = new int[] { 0, 58, 98, 105, 86 };
String[] countryName = { "Select", "England", "Germany", "India" };
String[] stateName = { "Select", "EnglandStateOne", "EnglandStateTwo", "EnglandStateThree" };
String[] cityName = { "Select", "EnglandCityOne", "EnglandCityTwo", "EnglandCityThree" };
ArrayAdapter<String> countryAdapter;
ArrayAdapter<String> stateAdapter;
ArrayAdapter<String> cityAdapter;
String countryIdByPosition;
int selectedPosition;

OnCreate Method:

spinnerMailingCountry = FindViewById<Spinner>(Resource.Id.spinnerCountry);
spinnerMailingState = FindViewById<Spinner>(Resource.Id.spinnerState);
spinnerMailingState.Enabled = false;
spinnerMailingCity = FindViewById<Spinner>(Resource.Id.spinnerCity);
spinnerMailingCity.Enabled = false;

countryAdapter = new ArrayAdapter<string>(this, Resource.Layout.support_simple_spinner_dropdown_item, countryName);
spinnerMailingCountry.Adapter = countryAdapter;

spinnerMailingCountry.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CountrySpinner_ItemSelected);
spinnerMailingState.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(StateSpinner_ItemSelected);
spinnerMailingCity.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CitySpinner_ItemSelected);

CountrySpinner_ItemSelected : Display state according to country.

private void CountrySpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
    var spinner = (Spinner)sender;
    selectedPosition = spinner.SelectedItemPosition;
    countryIdByPosition = countryId[selectedPosition].ToString();
    if(selectedPosition != 0) {
        stateAdapter = new ArrayAdapter<string>(this, Resource.Layout.support_simple_spinner_dropdown_item, stateName);
        //stateAdapter's data can get from your databse API with countryIdByPosition 

        spinnerMailingState.Enabled = true;
        spinnerMailingState.Adapter = stateAdapter;

    }
    else
    {
        spinnerMailingState.Enabled = false;
    }

}

StateSpinner_ItemSelected: Display city according to state.

private void StateSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
    var spinner = (Spinner)sender;
    selectedPosition = spinner.SelectedItemPosition;
    cityAdapter = new ArrayAdapter<string>(this, Resource.Layout.support_simple_spinner_dropdown_item, cityName);
    if (selectedPosition != 0)
    {
        spinnerMailingCity.Enabled = true;
        spinnerMailingCity.Adapter = cityAdapter;

        //cityAdapter' data can get from your databse API with selectedPosition 
        Console.WriteLine("-------------" + stateName[selectedPosition].ToString());
    }
    else
    {
        spinnerMailingCity.Enabled = false;
    }

}

CitySpinner_ItemSelected: Show the city you want.

private void CitySpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
    var spinner = (Spinner)sender;
    selectedPosition = spinner.SelectedItemPosition;
    Console.WriteLine("-------------" + cityName[selectedPosition].ToString());

}

When OnDestroy , unregister ItemSelected event:

protected override void OnDestroy()
{
    base.OnDestroy();
    spinnerMailingCountry.ItemSelected -= new EventHandler<AdapterView.ItemSelectedEventArgs>(CountrySpinner_ItemSelected);
    spinnerMailingState.ItemSelected -= new EventHandler<AdapterView.ItemSelectedEventArgs>(StateSpinner_ItemSelected);
    spinnerMailingCity.ItemSelected -= new EventHandler<AdapterView.ItemSelectedEventArgs>(CitySpinner_ItemSelected);
}

Upvotes: 1

Related Questions