0

I find this function in web android developer offficial

public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)

Now, I want to get sms from content://sms/sent , with thread_id of sms I will get only sms newest.

I will explain by example:

id   | thread_id |        phone     |       body

10       47
9        46 
8        47
7        45 
6        47 
5        43
4        45
3        42
2        41
1        47

For result I want to receive is:

 id   | thread_id |        phone     |       body

10       47
9        46     
7        45 
5        43
3        42
2        41

How must I do with query above? Thanks.

Hoang
  • 829
  • 11
  • 18
  • read about `max` and `group by` in sql(ite) – pskink Feb 15 '16 at 08:36
  • @pskink but seem function Cursor query above not use for Uri ? And you can give me a small example is very useful for because I also search more but can succes, and thanks for your comment. – Hoang Feb 15 '16 at 08:53
  • ah sorry, its taken from `ContentResolver#query`... so no, you cannot use `group by`, you have to iterate over the `Cursor` and find the max `id` in a loop – pskink Feb 15 '16 at 08:59

1 Answers1

0

You must use GROUP BY thread_id , see more details in my github repository SmsMessenger, you can get it by following query:

    Cursor cursor = MyApplication.getContext().getContentResolver().query(Uri.parse("content://sms")
            , null
            , "address IS NOT NULL) GROUP BY (thread_id"
            , null
            , null);
   if (cursor != null && cursor.moveToFirst()) { // must check the result to prevent exception
        do {
   String messageText = cursor.getString(cursor.getColumnIndexOrThrow("body"));
   System.out.println("messageText : "+messageText );
} while (cursor.moveToNext());
AliSh
  • 10,085
  • 5
  • 44
  • 76