Android 安卓短信:按线程id分组

Android 安卓短信:按线程id分组,android,android-contentprovider,Android,Android Contentprovider,我正在实现一个短信应用程序,到目前为止,我实现了获取所有信息(发送、接收、草稿)及其联系号码、线程id、联系id、日期、类型 这是我的密码: Uri mSmsinboxQueryUri = Uri.parse("content://sms"); Cursor cursor = _context.getContentResolver().query( mSmsinboxQueryUri, new S

我正在实现一个短信应用程序,到目前为止,我实现了获取所有信息(发送、接收、草稿)及其联系号码、线程id、联系id、日期、类型

这是我的密码:

Uri mSmsinboxQueryUri = Uri.parse("content://sms");
        Cursor cursor = _context.getContentResolver().query(
                mSmsinboxQueryUri,
                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);
            }
        }
    }

现在,我需要按线程id(具有相同线程id的消息)分隔这些消息。我怎样才能最好地做到这一点?谢谢

首先,我不会单独保存这些字符串

我要做的是像这样的课程:

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

    //c'tor
    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")); 
    }
}
现在,您可以在while循环中实例化
smsg
的对象,只要
光标长。moveToNext()
true
,并将其添加到您选择的
列表中


现在,您可以将所需
threadId
的所有消息复制到不同的
列表
,并按日期对其进行排序。这取决于您想如何处理它。

您可能在查询中查找“分组依据”。感谢您的回复,是的,我将按照此创建ArrayList,现在首先我需要将所有线程id获取到一个数组中,然后为每个id创建一个新的ArrayList。请详细说明按日期排序的方法。这是正确的方法吗?或者请您建议我一个消息应用程序的资源或参考源,该应用程序具有按对话列出所有消息、删除消息、删除对话等功能。请将Id保存到列表中。使用您将获得的所有功能,您还可以避免可能很容易发生的空指针异常。对于按日期排序,您必须阅读有关自定义比较器()的内容,因为日期是字符串对象,因此不是很好的可比性,您必须将其解析为DateTime对象(如果我没有记错的话)。抱歉,我没有任何好的源代码(即使是不好的)。但是不要犹豫,问一个关于这个问题的问题。我相信有人知道答案,或者至少可以指出答案。