根据Xamarin android中的另一个下拉列表动态填充下拉列表

时间:2019-04-01 11:19:00

标签: xamarin xamarin.android

我需要从Xamarin.Android(不需要在Xamarin.Forms中)获取选定国家/地区的州和相应的城市

我在Spinner控件中填充国家/地区,如下所示

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

现在,一旦选择了一个国家,我就需要在微调器中填充相应州的列表。这个流程也适用于城市。我该如何实现?

注意:州和城市列表是通过API调用从我的数据库中获取的。

1 个答案:

答案 0 :(得分:1)

  

现在,一旦选择了一个国家,我就需要在微调器中填充相应州的列表。这个流程也适用于城市。我该如何实现?

调用CountrySpinner_ItemSelected时,可以为设置ArrayAdapter。选择州时,可以将城市< / strong>。

axml:

ArrayAdapter

Activity.cs :添加测试数据(* stateName / cityName *)

<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" />

OnCreate 方法:

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;

CountrySpinner_ItemSelected :根据国家/地区显示状态。

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

StateSpinner_ItemSelected:根据州显示城市。

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

}

CitySpinner_ItemSelected:显示您想要的城市。

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

}

OnDestroy 时,取消注册ItemSelected事件:

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

}