Take the 2-minute tour ×

I am implementing a SMS App, till now i achieved to get all the messages(sent, received, drafts) with its contact number, thread id, contact id, date, type.

Here is my code:

Uri mSmsinboxQueryUri = Uri.parse("content://sms");
        Cursor cursor = _context.getContentResolver().query(
                new String[] { "_id", "thread_id", "address", "date", "body",
                        "type" }, null, null, null);

        String[] columns = new String[] { "address", "thread_id", "date",
                "body", "type" };
        if (cursor.getCount() > 0) {

            while (cursor.moveToNext()) {

                String address = null, date = null, msg = null, type = null, threadId = null;

                address = cursor.getString(cursor.getColumnIndex(columns[0]));
                threadId = cursor.getString(cursor.getColumnIndex(columns[1]));
                date = cursor.getString(cursor.getColumnIndex(columns[2]));
                msg = cursor.getString(cursor.getColumnIndex(columns[3]));
                type = cursor.getString(cursor.getColumnIndex(columns[4]));

                Log.e("SMS-inbox", "\nTHREAD_ID: "
                        + threadId + "\nNUMBER: " + address + "\nTIME: " + date + "\nMESSAGE: " + msg + "\nTYPE: " + type);

Now, i need to separate these messages by thread id (messages with same thread id). How can i best achieve that? Thanks!

share|improve this question
You might be looking for 'GROUP BY' in the query. –  wtsang02 Jan 26 '13 at 16:25
add comment

1 Answer

up vote 2 down vote accepted

I would not save those strings seperatly in the first place.

What I would do is a class like:

public class SmsMsg {
    private String address = null;
    private String threadId = null;
    private String date = null;
    private String msg = null;
    private String type = null;

    public SmsMsg(Cursor cursor) {
       this.address = cursor.getString(cursor.getColumnIndex("address"));
       this.threadId = cursor.getString(cursor.getColumnIndex("thread_id"));
       this.date = cursor.getString(cursor.getColumnIndex("date"));
       this.msg = cursor.getString(cursor.getColumnIndex("body"));
       this.type = cursor.getString(cursor.getColumnIndex("type")); 

now you can instantiate an object of SmsMsg in your while-loop as long cursor.moveToNext() is true and add it to a List of your choice.

You now could copy all messages of a desired threadId to a different List and sort it by date for example. That depends on what you want do do with it.

share|improve this answer
Thanks for the reply, Yes i will follow this creating a ArrayList<SmsMsg>, now first i need to get all the thread_id's to one array and then for each id i need to create a new ArrayList<SmsMsg>. Could you please elaborate on sorting by date. –  sukarno Jan 26 '13 at 16:44
Is this the right way am approaching or would you please suggest me a resource or reference source for a messaging App with features like listing all messages by conversation, deleting a message, delete a conversation etc.. –  sukarno Jan 26 '13 at 16:49
save the Id's to a List aswell. With all the functionality you will get, you also avoid Null-Pointer Exceptions, which could happen easily. –  SatelliteSD Jan 26 '13 at 16:50
for sort by date you have to read about custom comparators (javabeat.net/2007/09/sorting-custom-types-in-java) since date is a String-object and therefore not really good comparable, you will have to parse it to a DateTime-object (if I recall correctly). –  SatelliteSD Jan 26 '13 at 17:09
Sorry I don't have any good sources (not even bad ones). But don't hesitate to ask a question on SO. I am sure somebody knows an answer, or at least can point to it. –  SatelliteSD Jan 26 '13 at 17:22
show 3 more comments

Your Answer


