Ben Watson
Ben Watson

Reputation: 5531

Reverse the order of ListView display

I have a SimpleCursorAdapter to retrieve information from a SQLite database and put it into a ListView. It's all textbook stuff:

            new SimpleCursorAdapter(this, R.layout.viewexisting, c, from, to);

However, I would love for it to display the data in reverse. At the moment the oldest entry in the database appears at the top, and the newest appears at the bottom.

Upvotes: 2

Views: 4378

Answers (3)

Daniel Guillamot
Daniel Guillamot

Reputation: 863

Hmmm. The provided answer isn't quite right as that changes the order in which records come out of the database. Imagine the original question author has 50 records in his database and he wants to pull out the 10 newest items from the database everytime, but let the user change the way those 10 items are displayed. He would not want to change his SQL query.

Upvotes: 1

Tom Siwik
Tom Siwik

Reputation: 1012

Or resort Hashmap in reversed order. Good alternative if you have fixed Database-results.

Upvotes: 0

jtt
jtt

Reputation: 13541

The order of a list is dependent on how you structure your SQL Query. Specifically one that impacts this exactly is the Sort parameter inside of the SQLite query method. The last parameter is the column to sort by, depending on what this column is in your database it will sort it by that.. For example:

[database object].query(.., COL_NAME + " ASC");

COL_NAME is the name of the column and ASC stands for 'Ascending Order'

I believe its DESC for 'Decending Order'.

Remember that the column needs to be an integer, string, or a date. SQLite will use these attached with the Ascending Or Descending Parameter.

Upvotes: 4

Related Questions