16

我有一些代码对数据库执行两个查询并返回两个游标对象。有什么方法可以组合这两个游标,以便所有游标都ListView从两者中获取数据?

4

4 回答 4

14

MergeCursor(如果没有办法加入表格)。

于 2010-07-13T19:00:24.370 回答
9

仅供参考 - 使用 MergeCursor() 的示例

c = 包含来自 Contacts.CONTENT_URI 的联系人列的光标

private Cursor mergeCursorSubset(Cursor c) {

    int userMobile = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
        workMobile = ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;

    String storedNumber = ContactsContract.CommonDataKinds.Phone.NUMBER,
            displayName =ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            numberType = ContactsContract.CommonDataKinds.Phone.TYPE,
            contactKey = ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
            whereClausePre = contactKey+" = '",
            whereClausePost = "AND ("+numberType+" = '"+userMobile+"' OR "+numberType+" = '"+workMobile+"'";


    Uri lookupUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;;

    Cursor [] m = new Cursor[c.getCount()]; 

    if (c.moveToFirst())
        for (int k = 0; k<c.getCount();k++){            
            //Find the mobile numbers
            Cursor u = this.getContentResolver().query(lookupUri,
                                                    new String[]{displayName, storedNumber, numberType}
                                                    , whereClausePre+c.getString(c.getColumnIndex(Contacts.LOOKUP_KEY))+"') " 
                                                        + whereClausePost, null, null);
            int i = 0;
            if (u.moveToFirst())
                m[i++] = u;

        } //for Each key

    return new MergeCursor(m);

}
于 2011-10-15T06:34:52.710 回答
4

您也可以使用cwac-merge

cwac-merge:提供 MergeAdapter,一个将多个 View 或 ListAdapter 混合为单个 ListAdapter 的 ListAdapter。将此用于部分标题,将多个数据源混合在一起等。

于 2010-07-13T12:56:43.650 回答
1

查看MatrixCursor

也许这对你也有帮助Android - Database Table Join

于 2010-07-13T09:29:59.297 回答