首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用两个光标适配器的ListView?

使用两个光标适配器的ListView?
EN

Stack Overflow用户
提问于 2010-07-13 17:23:03
回答 3查看 12.4K关注 0票数 16

我有一些对数据库执行两个查询并返回两个游标对象的代码。有没有办法把这两个游标组合起来,让所有的ListView都从这两个游标中获取数据?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-14 03:00:24

MergeCursor可以做到这一点(如果无法连接表的话)。

票数 14
EN

Stack Overflow用户

发布于 2011-10-15 14:34:53

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

C=光标包含Contacts.CONTENT_URI中的联系人列

代码语言:javascript
复制
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);

}
票数 9
EN

Stack Overflow用户

发布于 2010-07-13 17:29:59

查看MatrixCursor

也许这也能帮到你Android - Database Table Join

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3235746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档