【问题标题】:Listview Order By NameListview 按名称排序
【发布时间】:2018-03-26 10:29:19
【问题描述】:

我使用 listview 制作了一个 Radio 应用程序。数据存储在 sqlite db 上。如果我使用“DB Browser for Sqlite”通过 sqlite 向应用程序添加新电台,则新电台排在最后,因为它按 id.example 编号排序100 排在第 100 位。我希望它按名称排序,例如以名称 A 开头的 Station。这是我的 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;
}

【问题讨论】:

  • 而且您从未尝试过搜索“sqlite 排序”之类的内容?
  • 当然,您没有正确搜索。我会给你一个提示你可以找到你的答案here
  • 为什么不直接使用其他SQLiteDatabase方法query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);

标签: java android sqlite sorting


【解决方案1】:

使用SQLiteDatabase.query()方法,无需了解SQL查询语句,即可轻松按站订购

Cursor cursor = db.query("TABLE_NAME",new String[]{"COLUMN1","COLUMN2","COLUMN3"},null,null,null,null,"COLUMN1 ASC");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多