Miaoulis Nikos
Miaoulis Nikos

Reputation: 182

Cannot Sort ListView item based on date which comes from SQLite and cursor adapter

I populate my listview with cursor adapter from SQLite. Date is inserted with CalendarView and it has the format as SQLite said yyyy-mm-dd based on the following:

cdr.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

   @Override
   public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) 
        {
            int d = dayOfMonth;
            int m = month + 1;
            int y = year;

            if (d < 10) {
                //String newDay = "0" + String.valueOf(d);
                mDate = String.valueOf(y) + "-" + String.valueOf(m) + "-" + String.valueOf(d);
            } else {
                //String newDay = "0" + String.valueOf(d);
                mDate = String.valueOf(y) + "-" + String.valueOf(m) + "-" + String.valueOf(d);
            }

            SaveTheGame.setEnabled(true);

        }
    });

Inside the Cursor Adapter I use the following to make the Date like dd-mm-yyyy:

    SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
    Date dateObj = null;
    try {
        dateObj = curFormater.parse(InitialDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    SimpleDateFormat postFormater = new SimpleDateFormat("dd/MM/yyyy");
    String newDateStr = postFormater.format(dateObj);

    textViewDate.setText(newDateStr);

I query the database with the following:

public Cursor getAll () {
    SQLiteDatabase db = this.getReadableDatabase();
    String buildSQL = "SELECT * FROM " + A_TABLE_ + " ORDER BY date DESC";
    return db.rawQuery(buildSQL, null);
}

the results I get from the queryare like the following:

09/05/2016 George
06/05/2016 John
04/05/2016 Mary
22/05/2016 Nick

which is not a descending order of dates

What I m doing wrong here?

Upvotes: 0

Views: 103

Answers (1)

Sohail Zahid
Sohail Zahid

Reputation: 8149

Change Query to SELECT * FROM " + A_TABLE_ + " ORDER BY date(date) DESC

Upvotes: 0

Related Questions