RoadXY
RoadXY

Reputation: 457

Android 2.3.3 spinner auto sorting

I'm filling a spinner with user-provided data from a SQLite database.

The problem is that on Android 3.0 and newer the spinner list occurrence is the same order as the data in the database (not sorted). However, in Android 2.3.3, the spinner contents are automatically sorted alphabetically. This means that the first item in my spinner is not the same item as the first item in my database/cursor.

A possible solution is to use getItemAtPosition(pos).toString() but since my spinner rows contains a combination of two columns from my database, this means I have to split the outcome of getItemAtPosition(pos).toString(), search the database for the first word, then search the results for the second word. Then I have to retrieve the corresponding ID and display the data belonging to this ID on screen. All of this code has to run every time the user selects another item on the spinner, which seems rather inefficient.

Most examples/tutorials I have found assume that the list order of the spinner and data are the same.

What am I doing wrong here?

Upvotes: 2

Views: 244

Answers (1)

Nicolai Buch-Andersen
Nicolai Buch-Andersen

Reputation: 593

I'm not sure what's going on. I've used spinners myself and never encountered that problem. Then again, I usually impose some sort of ordering myself, as I can't rely on the semi-random ordering of the database entries.

I suggest you add a "order by _id asc" (or desc) to your request. This will force Sqlite to sort the data according to the sequence in which they were added to the database.

Upvotes: 1

Related Questions