我将如何解决这个问题,我有COLUMN_ID,COLUMN_NAME和COLUMN_INTE
COLUMN_INTE持有长整数,我希望它显示在List视图中,它目前不会从最高到最低下降
答案 0 :(得分:2)
您需要使用ORDER BY子句针对您的表编写查询,该子句使用DESC关键字指示降序。这样的事情应该有效:
string query = "SELECT COLUMN_ID, COLUMN_NAME, COLUMN_INTE FROM MY_TABLE ORDER BY COLUMN_INTE DESC";
Cursor c = db.rawQuery(query, null);
String[] columns = new String[] { "COLUMN_ID", "COLUMN_NAME"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
try {
dataAdapter = new SimpleCursorAdapter(
this, android.R.layout.simple_list_item_2, c, columns, to, 0
);
ListView lv = (ListView) findViewById(R.id.MY_LISTVIEW);
lv.setAdapter(dataAdapter);
} catch (Exception ex) {
ex.printStackTrace();
}
答案 1 :(得分:0)
您也可以使用比较器进行排序。就像一个例子 -
类MySalaryComp实现Comparator {
@Override
public int compare(Empl e1, Empl e2) {
if(e1.getSalary() < e2.getSalary()){
return 1;
} else {
return -1;
}
}
}
答案 2 :(得分:0)
您有两种选择:使用为您排序的SQL命令,或者您可以获取整个列表并自行排序。
SQL命令排序:
// Create an object to store rows from the table
tableRowObject = new tableRowObject();
String query = "select * from STATISTICS order by COLUMN_INTE";
Cursor cursor = db.rawQuery(query, null);
if(cursor.getCount()<1) // COLUMN_INTE Not Found
{
cursor.close();
}
else
{
while(cursor.moveToNext())
{
// Get COLUMN_ID, COLUMN_NAME, COLUMN_INTE
int columnID = cursor.getInt(cursor.getColumnIndex("COLUMN_ID"));
String columnName = cursor.getString(cursor.getColumnIndex("COLUMN_NAME"));
int columnInte = cursor.getInt(cursor.getColumnIndex("COLUMN_INTE"));
// Add the variables to a new object
tableRowObject.add(columnID, columnName, columnInte);
}
cursor.close();
}
自己排序:
// Create an object to store rows from the table
tableRowObject = new tableRowObject();
String query = "select * from STATISTICS";
Cursor cursor = db.rawQuery(query, null);
if(cursor.getCount()<1) // COLUMN_INTE Not Found
{
cursor.close();
}
else
{
while(cursor.moveToNext())
{
// Get COLUMN_ID, COLUMN_NAME, COLUMN_INTE
int columnID = cursor.getInt(cursor.getColumnIndex("COLUMN_ID"));
String columnName = cursor.getString(cursor.getColumnIndex("COLUMN_NAME"));
int columnInte = cursor.getInt(cursor.getColumnIndex("COLUMN_INTE"));
// Add the variables to a new object
tableRowObject.add(columnID, columnName, columnInte);
}
cursor.close();
}
// Sort the object