Search code examples
javaandroidapiyelp

How to convert data from spinner to format API can use?


I have a spinner for an Android app that's populated with restaurant types in normal English defined in my strings.xml file as such (a few examples):

<string-array name="restaurantType">
    <item>American (Traditional)</item>
    <item>Asian Fusion</item>
    <item>Australian</item>
    <item>Barbeque</item>
    <item>Brazilian</item>
    <item>Buffets</item>
    <item>Burgers</item>
    <item>Chinese</item>
    <item>Fast Food</item>
    <item>German</item>

I take the selected item and convert it to string to send as a query to Yelp's API. On most, all I need to do to make it work properly is convert it to lower case and it works (Example: the spinner selection says Chinese but when the query is sent it's changed to chinese). Unfortunately it doesn't work for others like American (Traditional) that needs to be submitted as tradamerican. I don't want my spinner option to say tradamerican so is there a way to set the string-array items to display correctly but map to the API required format?


Solution

  • For your spinner, once the item is selected, you can grab it using spinner.getSelectedItem().toString(). You now have the string that you can use to check if it is American, and if it is, then send tradamerican to the yelp API