Question Detail

I am using SQLite in My Android Application for Store App Data in Local Database. I have fire below query to get all my table Data.

String selectQuery = "SELECT  * FROM " + TABLE_ALBUMS;

 

How can I get all Table Data in descending order? New data should be shown at the top of list 


Thread Reply

alex levine

- 10 months ago

Use this method in your code

to get descending order List

// Getting All Table Data
public List<DataClass> getAllData() {
    List<DataClass> dataList = new ArrayList<DataClass>();
    // Select All Data Query in descending order
    String selectQuery = "SELECT  * FROM " + TABLE_ALBUMS+ " ORDER BY " + KEY_ID + " DESC";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            DataClass album = new DataClass();
            album.setID(Integer.parseInt(cursor.getString(0)));
            album.setAlbumName(cursor.getString(1));
            album.setImagespathString(cursor.getString(2));
            album.setDescription(cursor.getString(3));
            album.setCaption(cursor.getString(4));
            album.setMemoryDate(cursor.getString(5));
            // Adding album to list
            dataList.add(album);
        } while (cursor.moveToNext());
    }
    // return Data list
    return dataList;
}