-9

I made a Radio app with listview.the data is stored on sqlite db.If i add new stations to app over sqlite with "DB Browser for Sqlite" new stations comes to last place because it order it by id.example number 100 is on 100 st place. i want it order by Name like Station beginning with name A on first place.This is my 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;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Murat
  • 1
  • 4
  • 2
    And you didn't ever try searching for something like "sqlite sorting"? – CL. Oct 14 '17 at 12:05
  • Of course, you didn't search properly. I will give you a hint you can find your answer [here](https://www.tutorialspoint.com/sqlite/sqlite_order_by.htm) – Kunu Oct 14 '17 at 12:48
  • why not just use the other `SQLiteDatabase` methods `query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);` – 0xDEADBEEF Oct 14 '17 at 13:25
  • 2
    Possible duplicate of [How do i order my SQLITE database in descending order, for an android app?](https://stackoverflow.com/questions/8948435/how-do-i-order-my-sqlite-database-in-descending-order-for-an-android-app) – Dmitry Oct 15 '17 at 07:30

1 Answers1

0

You can easily order by station without understanding the SQL query statements using SQLiteDatabase.query() method

Cursor cursor = db.query("TABLE_NAME",new String[]{"COLUMN1","COLUMN2","COLUMN3"},null,null,null,null,"COLUMN1 ASC");
0xDEADBEEF
  • 590
  • 1
  • 5
  • 16