Android:带有不断变化的 ArrayAdapter 的 Spinner - 如何识别?

发布于 2024-10-24 16:00:05 字数 677 浏览 4 评论 0原文

我想要三个 Spinner,其内容相互依赖。

例如,Spinner1 显示 {item1, item2},Spinner2 显示 {item3, item4} 或 {item5, item6},具体取决于在 Spinner1 上选择了 item1 还是 item2。 我想要的 Spinner 3 也是如此,它对 Spinner1 和/或 Spinner2 的变化做出反应。

对于后者,我必须首先确定 Spinner2 中显示了哪些可能的值集。

我的问题有点类似于 这个问题,但是我不知道拿到适配器后该怎么办。

这就是我到目前为止所拥有的:

ArrayAdapter adapter1 = (ArrayAdapter) spinner2.getAdapter();
if(items_spinner1[0].contentEquals(adapter1.getItem(0)))
{
    //...
}

我获取适配器,请求第一个值并将其与数组的第一个字符串值进行比较以识别它。对我来说,它一点也不优雅。有更简单的解决方案吗?

I want to have three Spinners with contents which depend on each other.

E.g. Spinner1 displays {item1, item2} and Spinner2 either {item3, item4} or {item5, item6} depending on whether item1 or item2 is selected on Spinner1.
The same I want for Spinner 3, which reacts to changes of Spinner1 and/or Spinner2.

For the latter, I have to determine first which of the possible value sets is shown atm in Spinner2.

My question is kind of similar to this question, but I don't know what to do after getting the adapter.

That's what I have so far:

ArrayAdapter adapter1 = (ArrayAdapter) spinner2.getAdapter();
if(items_spinner1[0].contentEquals(adapter1.getItem(0)))
{
    //...
}

I get the adapter, ask for the first value and compare it to the first String value of my Array to identify it. It doesn't at all seem elegant to me. Is there an easier solution?

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

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

发布评论

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

评论(1

蝶舞 2024-10-31 16:00:05

您说后面的旋转器的内容取决于前面旋转器中的选择,但您发布的代码仅取决于旋转器的内容

adapter1.getItem(0) 返回列表中的第一个项目,而不是当前选定的项目。要获取当前选定的项目,请使用微调器(而不是适配器)的 getSelectedItem() 方法。

例如,您可以在第一个 Spinner 的 onItemSelectedListener 中放置类似的内容(根据下面的评论进行编辑):

public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
    Object selectedItem = parent.getSelectedItem();

    // Do this if the first Spinner has a set of options that are
    // known in advance.
    if (/*selectedItem is something*/) {
        // Set up the second Spinner in some way
    } else if (/*selectedItem is something else*/) {
        // Set up the second Spinner in another way
    }

    // OR, if you need to do something more complex
    // that would cause too much clutter here, do this.
    fillSecondSpinner(selectedItem);
}

然后在第二个 Spinner 的 onItemSelectedListener 中放置类似的内容。使用 getSelectedItem() 从第一个和第二个 Spinner 中获取选定的项目(或者使用 getSelectedItemId() 对于第一个 Spinner 获取项目位置,对于第二个 Spinner 使用位置参数)。使用选定的项目设置第三个。

编辑:第二个 Spinner 的 OnItemSelectedListener 看起来像这样。

// This must be defined in the enclosing scope.
final Spinner firstSpinner;  // Must be final to be accessible from inner class.
Spinner secondSpinner;
// ...
secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener {
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        // Again, usually the selected items should be of
        // a more specific type than Object.
        Object firstSelection = firstSpinner.getSelectedItem();
        Object secondSelection = parent.getSelectedItem();

        fillThirdSpinner(firstSelection, secondSelection);
    }

public void onNothingSelected (AdapterView<?> parent) { }
});

You say the contents of the later spinners depend on the selection in the earlier ones, but the code you posted depends only on the contents of a spinner.

adapter1.getItem(0) returns the first item in the list, not the currently selected item. To get the currently selected item, use the spinner's (not the adapter's) getSelectedItem() method.

You could, for example, put something like this in your first Spinner's onItemSelectedListener (edited based on your comment below):

public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
    Object selectedItem = parent.getSelectedItem();

    // Do this if the first Spinner has a set of options that are
    // known in advance.
    if (/*selectedItem is something*/) {
        // Set up the second Spinner in some way
    } else if (/*selectedItem is something else*/) {
        // Set up the second Spinner in another way
    }

    // OR, if you need to do something more complex
    // that would cause too much clutter here, do this.
    fillSecondSpinner(selectedItem);
}

Then place something similar in the second Spinner's onItemSelectedListener. Get the selected items from the first and second Spinners using getSelectedItem() (or the item positions using getSelectedItemId() for the first and the position parameter for the second). Use the selected items to set up the third.

Edit: The OnItemSelectedListener for the second Spinner would look something like this.

// This must be defined in the enclosing scope.
final Spinner firstSpinner;  // Must be final to be accessible from inner class.
Spinner secondSpinner;
// ...
secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener {
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        // Again, usually the selected items should be of
        // a more specific type than Object.
        Object firstSelection = firstSpinner.getSelectedItem();
        Object secondSelection = parent.getSelectedItem();

        fillThirdSpinner(firstSelection, secondSelection);
    }

public void onNothingSelected (AdapterView<?> parent) { }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文