Reverse the order of ListView display

ε祈祈猫儿з 提交于 2019-12-10 10:31:19

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/5860732/reverse-the-order-of-listview-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!