如何设置数组列表项都被加入到反序? [英] How to set the arraylist item to be add in to reverse order?
问题描述
在我的申请,我会从我的数据库这个code存储所有可用的表的名称:
In My Application, I am going to store name of all the available tables from my database with this code:
public ArrayList<Object> showAllTable()
{
ArrayList<Object> tableList = new ArrayList<Object>();
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
if(cursor.getString(0).equals("android_metadata"))
{
//System.out.println("Get Metadata");
continue;
}
else
{
tableList.add(cursor.getString(0));
}
}
while (cursor.moveToNext());
}
cursor.close();
return tableList;
}
是我能够存储它。不过,虽然我要去到ListView控件来显示它,它会在最后创建的最后表现出如此的方式显示。我想中ListView中首先显示表作为最新创建的,显示的列表中。
Yes i am able to store it. But while i am going to display it in to ListView, it will display in such way that last created shown at last. I want to display the list of the table as Last created, display first in to ListView.
请帮我,我应该有我的code ++做的设置显示表这样的方式?
Please help me, What should I have to do in my code to set such way of displaying tables?
推荐答案
有几种方法可以做到这一点。最简单的可能是通过添加 ORDER BY
来改变你的SQL调用。这里的东西,你可以尝试一下:
There are several ways to do it. The easiest is probably to change your SQL call by adding ORDER BY
. Here's something you can try out:
要顺序递增的项目:
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name ASC";
或降:
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name DESC";
替代解决方案
另一种选择是做像你现在这样做,并通过光标
而不是去从上次的第一个项目。下面是一些伪code:
Another option would be to do it like you're doing it right now, and instead go through the Cursor
from the last to the first item. Here's some pseudo code:
if (cursor.moveToLast()) {
while (cursor.moveToPrevious()) {
// Add the Cursor data to your ArrayList
}
}
这篇关于如何设置数组列表项都被加入到反序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!