0

I am still a newbie in android developing and have a problem with my ToDo application.

At the moment my listview displaying all saved ToDo's is only sorted by the first two digits (the days!) of the date but the date is stored in this format: "dd.mm.yyyy"

My target is to sort my listview in ascending order by date (ToDoTable.COLUMN_TODO_START) and time (ToDoTable.COLUMN_TIME_START).

I use the following code at the moment:

public void fillData() {
    String[] from = new String[] { ToDoTable.COLUMN_TODO_NAME, ToDoTable.COLUMN_TODO_PLACE, ToDoTable.COLUMN_TODO_START, ToDoTable.COLUMN_TODO_END };
    int[] to = new int[] { R.id.todo_name, R.id.todo_place, R.id.todo_start };
    getLoaderManager().initLoader(0, null, this);
    adapter = new SimpleCursorAdapter(this, R.layout.todo_row_test2, null, from,
            to, 0);
    setListAdapter(adapter);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = { ToDoTable.COLUMN_ID, ToDoTable.COLUMN_TODO_NAME, ToDoTable.COLUMN_TODO_PLACE, ToDoTable.COLUMN_TODO_START, ToDoTable.COLUMN_TIME_START, ToDoTable.COLUMN_TODO_END, };
    CursorLoader cursorLoader = new CursorLoader(this,
            DataContentProvider.CONTENT_URI, projection, null, null, ToDoTable.COLUMN_TODO_START + " ASC" );
    return cursorLoader;
}

I would be very grateful if someone can help me with this issue especially in the point to sort the list by both criteria (date and time).

Thanks in advance.

Timitrov
  • 211
  • 1
  • 3
  • 14
  • Convert your Date object to Long . And then compare dates and sort it . Check this - http://stackoverflow.com/questions/12473550/how-to-convert-string-date-to-long-millseconds – randy Apr 17 '16 at 18:39
  • Can it be possible to integrate it together with my half-working ToDoTable.COLUMN_TODO_START + " ASC" part in the code? – Timitrov Apr 18 '16 at 17:21

2 Answers2

0

Best solution would be a database: For lots of data, feel free to use mysql, e.g. sqlite3. Import the library, put all data in the table and run a query:

reslt =mydatabase.find("select name,todo,time_start,time_end from todo_list order by time_start")

In mysql, the data and time is just one field. Use the TIMESTAMP in sql.

Bart Mensfort
  • 995
  • 11
  • 21
0

SQLite does not have a storage class set aside for storing dates and times. You can use Date And Time Functions.

So raw query can look like:

SELECT * FROM Table ORDER BY datetime(dateColumn) DESC Limit 1

Maxim G
  • 1,479
  • 1
  • 15
  • 23