I am building a cab booking app. This app needs to provide a feature to the cab driver to specify which city he will operate from. To enhance the experience, I need to provide a Select State drop down and then a Select City dropdown.
The states are listed in a string-array like this.
<string-array name="stateArray">
<item>Andaman and Nicobar Islands</item>
<item>Andhra Pradesh</item>
<item>Arunachal Pradesh</item>
<item>Assam</item>
</string-array>
The city’s are listed in a string-array like this.
<string-array name="AndhraPradesh">
<item>Adilabad</item>
<item>Adoni</item>
<item>Amadalavalasa</item>
<item>Amalapuram</item>
</string-array>
In the listeners I have
Setting the states is really easy in the layout xml file android:entries="@array/stateArray".
I have seen many other stackoverflow questions recommending the ArrayList and setAdapter. I am wondering if there is a relatively easy way to set the entries for the city too, programmatically depending on the state selected.
Use OnItemSelectedListener.
I assume you have two Spinners. It will be something like this.
Note that methods clear() and setData() are custom helper methods. There are no such methods in SpinnerAdapter.
Or you can implement objects and compare id’s instead if indexes. The code above is not tested and is just to show an idea.