Listview按名称排序

时间:2017-10-14 11:56:04

标签: java android sqlite sorting

我使用listview创建了一个Radio应用程序。数据存储在sqlite db上。如果我使用“DB Browser for Sqlite”将新工作站添加到sqlite上的新工作站,新工作站将到达最后一个位置,因为它按id.example数字排序100位于第100位。我希望它按名称排序,如站在名字A开头的第一名。这是我的StationDBHandler:

 public StationDBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    setForcedUpgrade();
}

/**
 *
 * @return list station
 */
public ArrayList<Station> getAllStations() {
    ArrayList<Station> stationArrayList = new ArrayList<>();

    String selectQuery = "SELECT  * FROM " + StationTable.stationTable;

    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            Station station = new Station();
            station.setId(cursor.getInt(cursor.getColumnIndex(StationTable.id)));
            station.setName(cursor.getString(cursor.getColumnIndex(StationTable.name)));
            station.setLogo(cursor.getString(cursor.getColumnIndex(StationTable.logo)));
            station.setGenre(cursor.getString(cursor.getColumnIndex(StationTable.genre)));
            station.setLongDesc(cursor.getString(cursor.getColumnIndex(StationTable.longDesc)));
            station.setStreamUrl(cursor.getString(cursor.getColumnIndex(StationTable.streamUrl)));

            stationArrayList.add(station);
        } while (cursor.moveToNext());
    }
    cursor.close();
    return stationArrayList;
}

1 个答案:

答案 0 :(得分:0)

您可以使用SQLiteDatabase.query()方法

轻松地按站排序,而无需理解SQL查询语句
Cursor cursor = db.query("TABLE_NAME",new String[]{"COLUMN1","COLUMN2","COLUMN3"},null,null,null,null,"COLUMN1 ASC");