0

I have serveral objects in my DB with three Strings attributes. One of the shows a date information, eg. 1.4.2016, 20.10.2017. I list these objects in a ListView, but before I want to sort them by their dates.

//
//Show all Data on ListView
//
private void displayListView() {


    Cursor cursor = datasource.fetchAllData();

    // The desired columns to be bound
    String[] columns = {
            DatabaseHelper.COLUMN_TITLE,
            DatabaseHelper.COLUMN_DESCRIPTION,
            DatabaseHelper.COLUMN_DEADLINE,
    };

    // the XML defined views which the data will be bound to
    int[] to = new int[] {
            R.id.processobject_title,
            R.id.processobject_description,
            R.id.processobject_deadline,
    };

    // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
    dataAdapter = new SimpleCursorAdapter(
            this, R.layout.listview_viewlayout,
            cursor,
            columns,
            to,
            0);

    ListView listView = (ListView) findViewById(R.id.listview);
    ArrayList <ProcessObject> arrayList = new ArrayList<>();
    // Assign adapter to ListView

    listView.setAdapter(dataAdapter);
    activateAddButton();
    OnItemClickListener();

};

I see two oppurtinities: sorting the cursor, or sorting the Array to[]. But I don't know how.

public Cursor fetchAllData(){

    Cursor cursor = database.query(DatabaseHelper.TABLE_DATABASE, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}
  • http://stackoverflow.com/questions/5927109/sort-objects-in-arraylist-by-date – Divyesh Patel Dec 08 '16 at 09:42
  • Store dates in "YYYY-MM-DD" format so that you can use `ORDER BY` in query or else store the dates as `INTEGER` and convert as and when required. [Sqlite Date and Time Datatype](https://www.sqlite.org/datatype3.html#date_and_time_datatype) – Monish Kamble Dec 08 '16 at 09:50
  • I did this, now I store the dates as "yyyy-MM-dd" in my database. But I don't know how to exactly apply ORDER BY. Could you give me an example? –  Dec 08 '16 at 11:23

1 Answers1

0

Solved it:

String orderBy =  DatabaseHelper.COLUMN_DEADLINE + " ASC";;
    Cursor cursor = database.query(DatabaseHelper.TABLE_DATABASE, columns, null, null, null, null, orderBy);