我对Android和Sqlite数据库编程非常陌生。 我想倒序排列我的游戏分数列。游戏得分列的类型是实数,当我尝试按gpoint和desc排序时,并没有正确地工作。请给我一些建议。代码如下:
public List<Score> getAllScores() {
List<Score> scoreList = new ArrayList<Score>();
String selectQuery = "SELECT * FROM " + TABLE_SCORES+" ORDER BY gpoint DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Score score = new Score();
score.setID(Integer.parseInt(cursor.getString(0)));
score.setPoint(cursor.getFloat(1));
score.setLetter(cursor.getString(3));
score.setDate(cursor.getString(2));
scoreList.add(0, score);
} while (cursor.moveToNext());
}
return scoreList;
}
我的测试插入内容如下:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
Log.d("Insert: ", "Inserting ..");
db.addScoreToDB(new Score((float)9.4, "S",formattedDate));
db.addScoreToDB(new Score((float)7.5, "S",formattedDate));
db.addScoreToDB(new Score((float)6.6, "S",formattedDate));
db.addScoreToDB(new Score((float)8.8, "S",formattedDate));
db.addScoreToDB(new Score((float)9.7, "S",formattedDate));
db.addScoreToDB(new Score((float)11.1, "S",formattedDate));
db.addScoreToDB(new Score((float)2.1, "S",formattedDate));
db.addScoreToDB(new Score((float)10.1, "S",formattedDate));
结果为:
12-24 15:52:24.065: D/插入:(9901):正在插入..
12-24 15:52:24.290: D/读取:(9901):正在读取所有联系人..
Id: 8 ,分数: 10.1 ,字母: S
Id: 6 ,分数: 11.1 ,字母: S
Id: 7 ,分数: 2.1 ,字母: S
Id: 3 ,分数: 6.6 ,字母: S
Id: 2 ,分数: 7.5 ,字母: S
Id: 4 ,分数: 8.8 ,字母: S
Id: 1 ,分数: 9.4 ,字母: S
Id: 5 ,分数: 9.7 ,字母: S
我想要的结果是:2.1,6.6,7.5.....以此类推
getReadableDatabase().query(TABLE_SCORES, null, null, null, null ,null, "gpoint desc");
- Toon Borgers