从独立微调器填充从属微调器

发布于 2024-11-29 21:54:27 字数 324 浏览 1 评论 0原文

我有一个 Spinner 对象,其中包含填充有美国 50 个州的字符串数组。

我还有一些数组,其中包含不同州的城市列表。我想要做的是将其他微调器选择的州的城市数组填充到依赖微调器中。

例如:

选择的第一旋转者 - 阿拉斯加

第二旋转者 - 安克雷奇、费尔班克斯、凯奇坎、科迪亚克。

另一个例子:

选择的第一旋转者 - 佛罗里达州

第二旋转者 - 塔拉哈西基韦斯特。

问题是我不确定如何根据所选的状态微调器填充依赖微调器。

我正在使用的数组适配器接受一个整数值,该值引用用于填充微调器的数组。

I have a Spinner object that contains an array of strings populated with the 50 states in the United States.

I also have some more arrays that contain lists of cities for the different states. What I am wanting to do is to populate the dependent spinner with the array of cities from the state that was selected by the other spinner.

For exampe:

First spinner selected - Alaska

Second spinner - Anchorage, Fairbanks, Ketchikan, Kodiak.

Another example:

First spinner selected - Florida

Second spinner - Key West, Tallahassee.

The problem is that I'm not sure how to populate the dependent spinner based on the state spinner selected.

The Array Adapter that I am using accepts an integer value that references the array that is used to populate the spinner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-12-06 21:54:27

我不知道您正在使用的确切 API 调用,但这是我使用的一般方法:

class CitySpinnerFactory {

    Map<String,Spinner> stateToCitySpinner = new HashMap<String,Spinner>();

    public void map(String state, Spinner citiesForState) {
        stateToCitySpinner.put(state,citiesForState);
    }

    public Spinner spinnerForState(String state) {
        return stateToCitySpinner.get(state);
    }

}

稍后您可能会这样做来填充城市和州

Spinner states = new Spinner();
Spinner cities = null;
CitySpinnerFactory cityFactory = new CitySpinnerFactory();

states.add("Alabama");
cities = new Spinner();
cities.add("Birmingham"); // city in AL
cities.add("Mobile"); // city in AL
cityFactory.map("Alabama",cities);

states.add("Alaska");
cities = new Spinner();
cities.add("Juneau"); // city in AK
cities.add("Anchorage");
cityFactory.map("Alaska",cities);
// ... add other states

稍后您可能会这样做来将城市移出

String state = states.spin(); // making up method to get a random state...
Spinner cities = cityFactory.spinnerForState(state);
String city = cities.spin();

I don't know the exact API calls you're using, but here's the general approach I'd use:

class CitySpinnerFactory {

    Map<String,Spinner> stateToCitySpinner = new HashMap<String,Spinner>();

    public void map(String state, Spinner citiesForState) {
        stateToCitySpinner.put(state,citiesForState);
    }

    public Spinner spinnerForState(String state) {
        return stateToCitySpinner.get(state);
    }

}

And later you might do this to populate the cities and states

Spinner states = new Spinner();
Spinner cities = null;
CitySpinnerFactory cityFactory = new CitySpinnerFactory();

states.add("Alabama");
cities = new Spinner();
cities.add("Birmingham"); // city in AL
cities.add("Mobile"); // city in AL
cityFactory.map("Alabama",cities);

states.add("Alaska");
cities = new Spinner();
cities.add("Juneau"); // city in AK
cities.add("Anchorage");
cityFactory.map("Alaska",cities);
// ... add other states

And later you might do this to get the cities out

String state = states.spin(); // making up method to get a random state...
Spinner cities = cityFactory.spinnerForState(state);
String city = cities.spin();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文