I query my SQL database to populate my spinners, I have no problem populating the spinner with a string array of the resulting Book Titles (this is a library style app). While getting the Book Titles into the spinner for selection is no problem, what is the best way to tie these titles back to their SQL _id's? I've been looking for a way to make spinners "multi-dimensional" but so far I don't see how.
Any help in the right direction would be much appreciated, thank you!
Look into using a SimpleCursorAdapter with your Spinner and a custom view for the child layouts. :)
Here is a tutorial.
What I did is used a multidimensional array for my spinner. It grabs things from
string[i][0]
and the ids are instring[i][1]
. Let me grab the code I used for it.Okay, so what this does is uses a class variable
String[][] BookResults
to hold both values, the name (in [][0]) and the id (in [][1]).BookResults[10][0]
would be the book name forBookResults[10][1]
's ID. The "search button" in this example shows how you'd get both values.You definitely want the SimpleCursorAdapter. You must include the _id in the select query. Here is an example...
... This will bind the _id to the spinner id. So when you select an item in the list using the onitemclicklistener like what I posted below. You will have the correct _id's associated with each of the names in the list...