I’m working on a SMS application and I faced some problems retrieving the data from the ContentProvider using the GROUP BY clause.
I have read a lot of threads of similar questions, but they still do not provide a working solution for me.
What I want, is to get the most recent SMS from every thread. To achive this I have to select the maximum value of DATE column, and group it by THREAD_ID, I believe.
Suppose the SMS table looks like this:
============================ THREAD_ID | MESSAGE | DATE | ============================ 1 | Hello | 555 1 | Hi | 666 2 | Test | 333 2 | Test 2 | 999
And the result should return the:
1 - Hi - 666 2 - Test 2 999
This is what I have so far, but the below code retrieves the most recent SMS from the whole table, not from specific category. It look like the GROUP BY clause is neglected.
String CONTENT_URI = "content://sms/";
Uri uri = Uri.parse(CONTENT_URI);
String selection = "date=(SELECT max(date) FROM sms )";
selection+=") GROUP BY (thread_id";
Cursor c = managedQuery(uri, null, selection, null, null);
if (c != null) {
while (c.moveToNext()) {
textView.append(c.getString(c.getColumnIndex(Column.THREAD_ID))
+ " - " + c.getString(c.getColumnIndex(Column.ADDRESS))
+ " - " + c.getString(c.getColumnIndex(Column.READ))
+ " - " + c.getString(c.getColumnIndex(Column.BODY))
+ " - " + c.getString(c.getColumnIndex(Column.DATE))
+ "\n");
}
c.close();
} else {
textView.setText("Cursor is null");
}
Thank you in advance, I would appreciate very much if you could help me find a solution for this case.
The GROUP BY clause is neglected because SELECT give only 1 value (in your code).
I don’t know very well SQL but I think this should work (sry, I don’t know exact syntax)