开发者

Android: Spinner with changing ArrayAdapter - how to identify?

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 wh开发者_StackOverflow中文版at 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?


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) { }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜